#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace 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();
}
}
Okay, so I'm trying to break away from goto statements and also, goto statements take forever. I'm almost finished with this, I just need help fixing it.
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
// prototypes
void roomB();
void roomA();
string choice;
int main(int argc, char *argv[])
{
// assuming you want to start in roomA:
roomA();
}
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();
}
}
- Prototypes look like type myFunction();
- Function declerations lose the ';' type myFunction() { //... }
- Don't declare your function inside of other functions (you delcared both inside of the main function)
- You should maybe have "Choose a room: 1) Dungeon, 2) Commonroom" then ask for an integer input (int choice) instead of asking for a string.
- Add some error code for invalid inputs (if room isn't 1 or 2.. do this)
I just joined up here and came across your topic. Your problem is that you are trying to use your functions before declaring them properly. The correct code would be this:
You are using functions as if they were gotos, which is a bad idea.
If the player keeps moving from room A to room B and back again, the computer has to remember where each call came from so it can "return" out of the function. It builds what's called a "call stack".
For example:
1 2 3 4 5 6 7 8 9 10 11
void foo()
{
cout << "foo";
}
int main()
{
foo(); // foo called here
// once foo "returns" / exits, the computer comes back here
cout << "after foo";
}
So by calling roomA from roomB and vice versa, you will force the computer to remember all the function calls. Eventually, if the user moves between them enough, you will run out of stack space and your program will crash.
An alternative is to do something more state based:
I actually greatly dislike them as they require you to keep multiple areas of code in sync (if you add a new room, you have to update the room function as well as all the switch statements that check the current room). I just showed it because it's simpler than some of the other alternatives.
A better way might be to have some kind of inheritance-based mechanism:
class Location
{
public:
virtual Location* Run() = 0;
};
class RoomA : public Location
{
public:
virtual Location* Run()
{
// do roomA stuff
// if user goes to room B
returnnew RoomB;
}
};
// .. same idea for RoomB
int main()
{
Location* current = new RoomA;
Location* next = 0;
while( current != 0 )
{
next = current->Run();
delete current;
current = next;
}
}