I'm refreshing my mind of my C++ skills,
and my older brother had the idea of creating a medieval text-based game!
He's making all the ideas and all that,
and I'm the code monkey programming it.
Here's the code:
main.cpp:

#include <iostream>
#include <string>
#include "CLASHinfo.h"
using namespace std;

int main()
{
    bool running = true;
    do {
        CLASH();
        cout << "\nRPG\n\n";
        cout << "Programmed by Xarver : Designed by Fishbowl444\n\n";
        cout << "[Press 1, Then Enter, to Start a New Adventure]\n"
            << "[Press 2, Then Enter, to Load an Adventure]\n"
            << "[Press z, Then Enter, to Quit]\n\n";
        char entry;
        cin >> entry;
        if(entry == 1)
        {

        }
        cin.get();
        running = false;
    }while(running == true);

    return 0;
}

CLASHinfo.h:

#include <iostream>
#include <string>
using namespace std;

void YesorNofunc()
{
    cout << "Are you sure?\n\n"
            << "[1] Yes\n"
            << "[2] No\n\n";
     char YesorNo;
     cin >> YesorNo;
     if(YesorNo == 1)
     {

     }else if(YesorNo == 2)
     {

     }else {
         cout << "Invalid; Try Again.";
     }
}

void CLASH()
{
    cout
        << " ::::::::  :::            :::      ::::::::  :::    ::: \n"
        << ":+:    :+: :+:          :+: :+:   :+:    :+: :+:    :+: \n"
        << "+:+        +:+         +:+   +:+  +:+        +:+    +:+ \n"
        << "+#+        +#+        +#++:++#++: +#++:++#++ +#++:++#++ \n"
        << "+#+        +#+        +#+     +#+        +#+ +#+    +#+ \n"
        << "#+#    #+# #+#        #+#     #+# #+#    #+# #+#    #+# \n"
        << " ########  ########## ###     ###  ########  ###    ### \n";
}

The purpose of the YesorNofunc is it provides two parameters.
For example, if the user enters 1, it will do the first action.
If the user enters 2, it will do the second one.
For example, YesorNofunc(firstaction(), sencondaction());
The firstaction would be a function that does an action, for example:
void firstaction
{
cout << "Pie.";
}
if the user entered 1, it would print "Pie."
I'm not sure how to do this...
I want it when the user enters 1,
it continues and goes onto the new character screen.
If the user enters 2, it would go back to the main menu...
But I want this function re-usable, meaning
if you are at a shop and you are going to buy something,
before you buy it it asks, "Are you sure?"
And I can re-use it for any other thing.

I'm confused;
Can this be done in OOP?
I have been studying OOP a little...
I would like this to work, or
just any alternative for the goto in my situation.

Any help appreciated!

~ Xarver

Recommended Answers

All 5 Replies

Note: I'm not near a C++ compiler, but hopefully the below logic will be helpful to you.

Create an interface that has two options for every subclass of its type--

class DoSomething{
 
    public:
        DoSomething(){}
        virtual void option1() const = 0;
        virtual void option2() const = 0;
};

-- then create some subclasses--

class DoNothing : public DoSomething{

    public:
        DoNothing(){}
        virtual void option1() const {}
        virtual void option2() const {}

};

Then change your function to (by default) do nothing if no parameter is given,
or when an appropriate DoSomething object is given it will perform the action--

void YesorNofunc(const DoSomething& action = DoNothing() )
{
    cout << "Are you sure?\n\n"
            << "[1] Yes\n"
            << "[2] No\n\n";
     char YesorNo;
     cin >> YesorNo;
     if(YesorNo == 1)
     {
           action.option1();
     }else if(YesorNo == 2)
     {
           action.option2();
     }else {
         cout << "Invalid; Try Again.";
     }
}

-- hopefully this will give you an idea of what to do to make your method reusable. Have fun =)

Edit: Just got to a compiler, here's a running example--

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::flush;

class DoSomething{
 
    public:
        DoSomething(){}
        virtual void option1() const = 0;
        virtual void option2() const = 0;
};

class DoNothing : public DoSomething{

    public:
        DoNothing(){}
        virtual void option1() const {}
        virtual void option2() const {}

};

class Speak : public DoSomething{

	public:
		Speak(){};
		virtual void option1() const {
			cout << "Whoo! O_O" << endl;
		}

		virtual void option2() const {
			cout << "Oh no >_<" << endl;
		}

};

void YesorNofunc(const DoSomething& action = DoNothing() )
{
    cout << "Are you sure?\n\n"
            << "[1] Yes\n"
            << "[2] No\n\n";
     int YesorNo;
     cin >> YesorNo;
     if(YesorNo == 1)
     {
           action.option1();
     }else if(YesorNo == 2)
     {
           action.option2();
     }else {
         cout << "Invalid; Try Again.";
     }
}

int main(){
	YesorNofunc(Speak());
	cin.ignore();
	cin.get();
	return 0;
}

Rather than having YesorNofunc() call another function, it would probably be easier to have it return int. You could declare two constants (or use #define) YES and NO. Then whenever you need a yes or no answer, you could use an if/else statement:

const int YES = 1;
const int NO = 2;

if( YesorNofunc() == YES ) {
// ... do yes stuff, call a function or w/e ...
}
else {
// ... do no stuff ...
}

You might want to rename your function too; having "func" on the end of a function name doesn't make much sense. It should be obvious a name is a function. If you need "func" to remind yourself it is a function and not a variable, something is wrong with your naming.

Thank you Alex, but I have sort of a problem.
I run your program, it works good.
It try to test if it works with mine, and it doesn't. :(
I tried testing it, and when you press 1 to start a new adventure,
it does nothing and closes...

main.cpp:

#include <iostream>
#include <string>
#include "CLASHinfo.h"
using namespace std;

int main()
{
    bool running = true;
    do {
        CLASH();
        cout << "\nRPG\n\n";
        cout << "Programmed by Xarver : Designed by Fishbowl444\n\n";
        cout << "[Press 1, Then Enter, to Start a New Adventure]\n"
            << "[Press 2, Then Enter, to Load an Adventure]\n"
            << "[Press z, Then Enter, to Quit]\n\n";
        char entry;
        cin >> entry;
        cin.ignore();
        if(entry == 1)
        {
            YesorNofunc(Speak());
            cin.ignore();
            cin.get();
        }
        running = false;
    }while(running == true);

    return 0;
}

CLASHinfo.h:

#include <iostream>
#include <string>
using namespace std;

class DoSomething
{

    public:
        DoSomething(){}
        virtual void option1() const = 0;
        virtual void option2() const = 0;
};

class DoNothing : public DoSomething
{

    public:
        DoNothing(){}
        virtual void option1() const {}
        virtual void option2() const {}

};

class Speak : public DoSomething
{

	public:
		Speak(){};
		virtual void option1() const {
			cout << "Whoo! O_O" << endl;
		}

		virtual void option2() const {
			cout << "Oh no >_<" << endl;
		}

};

void YesorNofunc(const DoSomething& action = DoNothing() )
{
    cout << "Are you sure?\n\n"
            << "[1] Yes\n"
            << "[2] No\n\n";
     int YesorNo;
     cin >> YesorNo;
     if(YesorNo == 1)
     {
           action.option1();
     }else if(YesorNo == 2)
     {
           action.option2();
     }else {
         cout << "Invalid; Try Again.";
     }
}

void CLASH()
{
    cout
        << " ::::::::  :::            :::      ::::::::  :::    ::: \n"
        << ":+:    :+: :+:          :+: :+:   :+:    :+: :+:    :+: \n"
        << "+:+        +:+         +:+   +:+  +:+        +:+    +:+ \n"
        << "+#+        +#+        +#++:++#++: +#++:++#++ +#++:++#++ \n"
        << "+#+        +#+        +#+     +#+        +#+ +#+    +#+ \n"
        << "#+#    #+# #+#        #+#     #+# #+#    #+# #+#    #+# \n"
        << " ########  ########## ###     ###  ########  ###    ### \n";
}

Keep in mind this is testing...
I'm not going to "steal" your code.
If you fix this program, can you explain how it works?
I'm fairly new to Object Oriented Programming. :)

Any help appreciated!

P.S. Evan:
I know that would be easier,
but the whole purpose of this project is to expand my knowledge,
and get better in C++.


~ Xarver

You should probably change entry's data type from char to int and see if you get better results.

-Alex

Wow, I must be the worst debugger in the world... -_-

I just put the ' ' around the 1...

Rep+ :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.