I'm almost done with my "Text Adventure" game, but the last thing I need to worry about is getting my game to end once I hit the exit door. I tried break but that doesn't seem to work; how about while statements?
cout << "\n\nPick a door to enter by typing the direction it is in ";
cout << "(N/E/S/W): ";
cin >> direction;
cout << endl;
if (align == 0)
{
if (direction == 'N')
{
monsterRoom(banana, orange, monster);
cout << "\n\n";
cout << name << ", you are in a room with 4 doors." << endl;
cout << "You are carrying " << banana << " bananas and ";
cout << orange << " oranges." << endl;
cout << endl;
}
else if (direction == 'S')
{
genieRoom(banana, orange);
cout << "\n\n";
cout << name << ", you are in a room with 4 doors." << endl;
cout << "You are carrying " << banana << " bananas and ";
cout << orange << " oranges." << endl;
cout << endl;
}
else if (direction == 'E')
{
drawPicture();
cout << "\n\n";
cout << name << ", you are in a room with 4 doors." << endl;
cout << "You are carrying " << banana << " bananas and ";
cout << orange << " oranges." << endl;
cout << endl;
}
else if (direction == 'W')
{
ExitRoom(banana, orange, score);
break;
}
else
{
cout << "\n\nPick a door to enter by typing the direction it ";
cout << "is in (N/E/S/W): ";
cin >> direction;
}
}
if (align == 1)
{
if (direction == 'N')
{
ExitRoom(banana, orange, score);
break;
}
else if (direction == 'S')
{
monsterRoom(banana, orange, monster);
cout << "\n\n";
cout << name << ", you are in a room with 4 doors." << endl;
cout << "You are carrying " << banana << " bananas and ";
cout << orange << " oranges." << endl;
cout << endl;
}
else if (direction == 'E')
{
genieRoom(banana, orange);
cout << "\n\n";
cout << name << ", you are in a room with 4 doors." << endl;
cout << "You are carrying " << banana << " bananas and ";
cout << orange << " oranges." << endl;
cout << endl;
}
else if (direction == 'W')
{
drawPicture();
cout << "\n\n";
cout << name << ", you are in a room with 4 doors." << endl;
cout << "You are carrying " << banana << " bananas and ";
cout << orange << " oranges." << endl;
cout << endl;
}
else
{
cout << "\n\nPick a door to enter by typing the direction it ";
cout << "is in (N/E/S/W): ";
cin >> direction;
}
}
return 0;
}
break; is not required here. It applies to loop and switch only.
After the if is done the function (whatever it is) just ends. if the function is main then the program exits.
if you're in a [deep] recursion you can use exit(0):