| | |
Clash RPG Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 10
Reputation:
Solved Threads: 0
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:
CLASHinfo.h:
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
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:
c++ Syntax (Toggle Plain Text)
#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; }
c++ Syntax (Toggle Plain Text)
#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
Last edited by Xarver; Oct 28th, 2008 at 10:56 am.
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--
-- then create some subclasses--
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--
-- 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--
Create an interface that has two options for every subclass of its type--
c++ Syntax (Toggle Plain Text)
class DoSomething{ public: DoSomething(){} virtual void option1() const = 0; virtual void option2() const = 0; };
-- then create some subclasses--
c++ Syntax (Toggle Plain Text)
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--
c++ Syntax (Toggle Plain Text)
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--
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Alex Edwards; Oct 28th, 2008 at 12:45 pm.
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:
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.
c++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Oct 2008
Posts: 10
Reputation:
Solved Threads: 0
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:
CLASHinfo.h:
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
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:
c++ Syntax (Toggle Plain Text)
#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; }
c++ Syntax (Toggle Plain Text)
#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"; }
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
Last edited by Xarver; Oct 28th, 2008 at 6:58 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: I Need Help with Cases in C++.
- Next Thread: Advanced cout??
Views: 364 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





