help witha if statement

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
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

#include <cstdlib>
#include <iostream>
#include <string>

using namespace 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;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;

return is being used when you call a function.
Last edited on
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.

I hoped I helped!
Last edited on
whoops I also forgot to add a break statement breaks the loop. It's one way to get out of a loop.
thanks for the quick reply, i tried using that goto and got this error

label `Step2' used but not defined
break(); //add your break statement here

i have never done this before i tried

brake() string Step2; but that didn't work, what am i missing?
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";          


Using goto is highly inadvisable. Use some sort of loop instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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";
  }
  else if (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.
Last edited on
thanks vlad that worked a treat

so do tells it what to do

the if say if it is wrong print unknown command please try again

what does the while do?
Topic archived. No new replies allowed.