I am relatively new to C++, I have searched everywhere for a answer but have found the explanations confusing or not the one I was looking for. I am trying to make the code on line 25 go back to "string Step2" on line 15 if the else is met. Any help is much appreciated
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
int main(int argc, char *argv[])
{
string Step1;
cout << "Welcome to the game, what is your name \n";
cin >> Step1;
cout << "welcome " << Step1 << " Now you're adventure begins \n";
string Step2;
cout << "you find your self in a dark room what would you like to do \n Options: \n look \n";
cin >> Step2;
if (Step2 == "look")
{
cout << "you see a table and a door\n";
}
else
{
cout << "unknown command please try again\n";
return string Step2;
}
system("PAUSE");
return EXIT_SUCCESS;
}
string Step1;
cout << "Welcome to the game, what is your name \n";
cin >> Step1;
cout << "welcome " << Step1 << " Now you're adventure begins \n";
step2:
string Step2;
cout << "you find your self in a dark room what would you like to do \n Options: \n look \n";
cin >> Step2;
if (Step2 == "look")
{
cout << "you see a table and a door\n";
}
else
{
cout << "unknown command please try again\n";
goto step2;
}
system("PAUSE");
return EXIT_SUCCESS;
The keyword return returns a value from a function. What you are wanting is the goto statement. So you would write
goto string Step2;
Be warned the goto statement is highly recommended NOT to use. If you use a lot in a function it can cause hard to trace bugs. The recommended way to go back to something is somehow put it in a loop. You could say to aviod a goto statement say
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while(1)
string Step2;
cout << "you find your self in a dark room what would you like to do \n Options: \n look \n";
cin >> Step2;
if (Step2 == "look")
{
cout << "you see a table and a door\n";
break(); //add your break statement here
}
else
{
cout << "unknown command please try again\n";
//no need for the goto statement
}
You can use whatever method you want but I recommend the loop method(2nd choice). And also if you can try to aviod system("PAUSE"). In fact don't use system anything. It is a bad habit and has huge security risk.
You need neither goto nor if. You should use while or do-while construction. So instead of
1 2 3 4 5 6 7 8 9 10 11 12
string Step2;
cout << "you find your self in a dark room what would you like to do \n Options: \n look \n";
cin >> Step2;
if (Step2 == "look")
{
cout << "you see a table and a door\n";
}
else
{
cout << "unknown command please try again\n";
return string Step2;
}
you could write for example the following
1 2 3 4 5 6 7 8 9 10
string Step2;
do
{
cout << "you find your self in a dark room what would you like to do \n Options: \n look \n";
cin >> Step2;
if ( Step2 != "look" ) cout << "unknown command please try again\n";
} while ( Step2 != "look" );
cout << "you see a table and a door\n";
string choice;
bool again;
do
{
again = false;
cout << "What do you want to do?";
getline(cin, choice);
if (choice == "look")
{
cout << "You see a table and a door\n";
}
elseif (choice == "look at table")
{
cout << "You see a key\n";
}
else
{
cout << "Unknown command. Try again: ";
again = true;
}
}
while (again);
Of course, it doesn't really help to break the loop after he/she merely looks at things, but I imagine you can work that out.