Using functions for a text based RPG

Okay so after having asked this in the General C++ forum they told me i should ask it here because of my limited knowledge, (http://cplusplus.com/forum/general/29165/)

I need to know how to jump around in my code to be able to do something sort of like this:

You start in room A
you have the choice of going to room B
In room B you will have the option to go back to room A or continue to another room, etc etc.

this is what i have been given so far but i get an error.

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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string choice;

void roomA()
{
 cout << "You are in room A" << endl;
 cin >> choice;
  if ( choice == "roomB" )
  {
    roomB();
  }
}
void roomB()
{
 cout << "You are in room B" << endl;
 cin >> choice;
  if ( choice == "roomA" )
  {
    roomA();
  }
}

int main(int argc, char *argv[])
{
    roomA()
    system("PAUSE");
    return EXIT_SUCCESS;
}


OR (im not sure how i was supposed to use the snippet i was given)

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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string choice;
void roomB();
void roomA();

int main(int argc, char *argv[])
{
    
void roomA();
{
 cout << "You are in room A" << endl;
 cin >> choice;
  if ( choice == "roomB" )
  {
    roomB();
  }
}
void roomB();
{
 cout << "You are in room B" << endl;
 cin >> choice;
  if ( choice == "roomA" )
  {
    roomA();
  }
}


The error i received from the latter was

1
2
  [Linker error] undefined reference to `roomA()' 
  [Linker error] undefined reference to `roomB()'
you recieved an error because you defined the roomA(); and roomB(); functions inside int main();.

you should put that outside of "main"


int main(int argc, char *argv[])
{
roomA();
roomB();

}

void roomA() ------------------------> Also leave out the ; during function definition
{
cout << "You are in room A" << endl;
cin >> choice;
if ( choice == "roomB" )
{
roomB();
}

void roomB() ------------------------> Also leave out the ; during function definition

{
cout << "You are in room B" << endl;
cin >> choice;
if ( choice == "roomA" )
{
roomA();
}

}



And about jumping around in code, you can use the goto(); function but... you already are jumping from function to other function with that code you scripted. Its alot better than goto();. So your set.
Last edited on
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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string choice;
void roomB();
void roomA();



int main(int argc, char *argv[],void roomA(), void roomB())
{
    roomA();
    roomB();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


void roomA()
{
 cout << "You are in room A" << endl;
 cin >> choice;
  if ( choice == "roomB" )
  {
    roomB();
  }
}
void roomB()
{
 cout << "You are in room B" << endl;
 cin >> choice;
  if ( choice == "roomA" )
  {
    roomA();
  }
}


So thats what i put for my code and now i dont get a compilation error but when the console opens i get an error report! >:U
I found it, wowww int main(int argc, char *argv[],void roomA(), void roomB())
i
i was messing around trying to figure it out on my own :P those dont belong there...

You have been a TREMENDOUS help =]
Last edited on
Yeah... your adding extra arguments to be passed to int main...

Technically you don't need int argc, or char *argv[] either, because nothing is being passed to main. So main's parameter list can be empty int main(){

Yeah your welcome.
Last edited on
And about jumping around in code, you can use the goto(); function but... you already are jumping from function to other function with that code you scripted. Its alot better than goto();. So your set.


Don't use goto...it's definitely not needed in this situation.

Also, the way you have it, if someone goes back and forth between room a and b, your program will eventually crash due to recursion. I'd suggest making a variable like "current place" that stores the room, then call the appropriate function for that room, which would then return the next area to go to.
Topic archived. No new replies allowed.