Oct 8, 2011 at 4:39pm UTC
Hi. I'm fairly new to C++. I have been self learning for a while (getting the jist of it and I've done things such as If statements, switch case statements and so on). Anyways I've decided to do a little text based rpg game. However in the code I used "goto" a fair amount which is frowned upon.
The question I would like to ask is, is it possible to create events/procedures that can be called at certain stages of the game.
For example if I had the following code:
cout << "Your health: " << health << endl;
cout << "Your ammo: " << ammo << endl;
cout << "Your strength: " << strength << endl;
cout << "Your intelligence: " << intelligence << endl;
cout << "Your luck: " << luck << endl;
Is it possible to call up this bit of code when I need it and if so how?
Thanks in advance.
Oct 8, 2011 at 4:47pm UTC
Use a function, they're a whole lot better than goto statements.
Last edited on Oct 8, 2011 at 4:48pm UTC
Oct 8, 2011 at 4:51pm UTC
Would I still be able to do things like If statements etc in those? And if so how do I do them? :)
Oct 8, 2011 at 4:59pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <iostream>
using namespace std;
char again;
int health = 100;
int ammo = 500;
int strength = 250;
int intelligence = 44;
int luck = 12;
int watermelon();
int main (void ){
cout << ":D" << endl;
watermelon();
return 0;
}
int watermelon(){
cout << "Your health: " << health << endl;
cout << "Your ammo: " << ammo << endl;
cout << "Your strength: " << strength << endl;
cout << "Your intelligence: " << intelligence << endl;
cout << "Your luck: " << luck << endl;
cout << "Play again?(Y or N)" << endl;
cin >> again;
if (again == 'Y' || again == 'y' )
{
watermelon();
}
else (again == 'N' || again == 'n' );
cout << "Bye!" ;
return 0;
}
Hope this helps... :)
(Sorry for my weird naming... I felt like being random)
Last edited on Oct 8, 2011 at 5:00pm UTC
Oct 8, 2011 at 5:03pm UTC
Oh my god... I feel abused by god xD
I actually did the int thing
int watermelon() {
}
and
watermelon ();
*with my names obviously* however I didn't put int watermelon() at the start q.q
Thankyou so much :D This has helped me so much. :)