if/else and goto

I was messing around with user input in code, and wanted to see if an invalid statement would reset back to the original prompt again. However...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{ cout << "What is your favourite colour? ";
string colour;
cin >> colour;
cout << "That's groovy, man! Turns out "; cout << colour; cout << " is my favourite colour, as well! ";
loop:
    cout << "Are you an \n"; int adventurer; cout << "adventurer, "; int politician; cout << "policitian, or "; int dreamer; cout << "dreamer? ";
string mystring;
cin >> mystring;
if (mystring == "adventurer")
   cout << "That's far out, man. You have +2 in all attributes.\n";
if (mystring == "dreamer")
    cout << "That's dreamy, man. You have +5 in skill.\n";
if (mystring == "policitian")
    cout << "That's psychadelic, man. You have +5 in IQ.\n";
else {cout << "That's not an option, man. Pick again." goto loop}
return 0;
}


...after implementing the "goto" statement, when executed the answer will always be "That's not an option, man. Pick again.", even if you typed in adventurer, dreamer or politician (with those, you'll receive the original intended response and then the else statement).
I've known the goto function was a fishy thing for a while, so I was wondering if there was a better way of going about this. Thank you in advance!
Last edited on
have you tried the do while?
1
2
3
4
5
6
7
8
9
10
11
12
do {
cout << "Are you an \n"; int adventurer; cout << "adventurer, "; int politician; cout << "policitian, or "; int dreamer; cout << "dreamer? ";
string mystring;
cin >> mystring;
if (mystring == "adventurer")
   cout << "That's far out, man. You have +2 in all attributes.\n";
if (mystring == "dreamer")
    cout << "That's dreamy, man. You have +5 in skill.\n";
if (mystring == "policitian")
    cout << "That's psychadelic, man. You have +5 in IQ.\n";
else cout << "That's not an option, man. Pick again.";
} while(mystring != "adventurer" && mystring != "dreamer" && mystring != "politician");
Last edited on
Thanks for the reply!
If I try using the do while suggested in your code, I get the error of the "mystring" not being defined within the scope of int main (); if I define it outside the parameters of the do while as well as inside, then no error messages show up, however I still get the same exact results of my goto loop scenario.

Perhaps there is some function missing or am I using too many complex options at once?

Last edited on
Oh um try heroine(cin, mystring);

EDIT::
whoops was on my phone lol

getline(cin, mystring);

Last edited on
closed account (Dy7SLyTq)
Never ever ever use goto ever. every time you use it someone is forced to write an rpg in batch
you should be using if else statments. In your code you have this.

5
6
7
8
9
10
11
12
if (mystring == "adventurer")
   cout << "That's far out, man. You have +2 in all attributes.\n";
if (mystring == "dreamer")
    cout << "That's dreamy, man. You have +5 in skill.\n";
if (mystring == "policitian")
    cout << "That's psychadelic, man. You have +5 in IQ.\n";
else cout << "That's not an option, man. Pick again.";
} while(mystring != "adventurer" && mystring != "dreamer" && mystring != "politician");


lets say mystring == "adventurer"; the first if statement is evaluated and it equals true. So the cout statement executes. the next if statemnt is evaluated and evaluates to false because mystring does not equal dreamer. So the cout statement is skipped. Then the third if statment is evaluated and evaluates to false because mystring does not equal "policitian", but this if statement has an else statement so the else statment is executed and "That's not an option, man. Pick again."; is output to the screen.

You are also missing a couple ; else {cout << "That's not an option, man. Pick again."; goto loop;}

when you use cin >> you leave a newline in the input stream. so you should follow each call to cin >> with a cin.ignore() statement, to get rid of it.
Last edited on
I was just showing him how to use a do/while and don't use the goto I left that out for a reason.
and yeah he just needs to get rid of that else at the end of make them all else if
Last edited on
STYLE ALERT

Things you should consider.

#1: INDENT. Don't be afraid of whitespace. Obeying simple indentation rules and adding a few blank lines here and there makes your code infinitely easier to read and understand. It'll also help you catch mismatching {} errors.

Cramming as much as you can on as few lines as possible does not do anything for your program other than make it harder to read.

1
2
3
4
5
6
7
8
// BAD
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{ cout << "What is your favourite colour? ";
string colour;
1
2
3
4
5
6
7
8
9
10
// BETTER
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    cout << "What is your favourite colour? ";
    string colour;


#2: Don't embed variable definitions in output statements. It makes them very hard to see and clutters what you're doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
// BAD
cout << "Are you an \n"; int adventurer; cout << "adventurer, "; int politician; cout << "policitian, or "; int dreamer; cout << "dreamer? ";

// BETTER
int adventurer;
int politician;
int dreamer;

cout << "Are you an\n"
        "adventurer, politician, or dreamer? ";

// (note I added a line break in the output there for clarify but you can cram it all on one
//  line if you want.  But I don't recommend it.  Clarity is huge!) 



Though I'm not sure why you think you need those adventurer, etc variables anyway....


#3: Don't duplicate code. You're already checking for the strings "adventurer", "dreamer", etc. If you're already doing it once, don't do it again!.

#4: You want else/if chains here. else only binds to the one previous if. It does not bind to all of them. In your code example... if the player inputs "adventurer", it will display the adventurer message as well as the "pick again" message because the pick again message will display if the user inputs anything other than "politician"

(also you are spelling politician wrong in a few places).

Example of #3 & #4:


1
2
3
4
5
6
7
8
9
10
11
12
13
// BAD
do {
cout << "Are you an \n"; int adventurer; cout << "adventurer, "; int politician; cout << "policitian, or "; int dreamer; cout << "dreamer? ";
string mystring;
cin >> mystring;
if (mystring == "adventurer")
   cout << "That's far out, man. You have +2 in all attributes.\n";
if (mystring == "dreamer")
    cout << "That's dreamy, man. You have +5 in skill.\n";
if (mystring == "policitian")
    cout << "That's psychadelic, man. You have +5 in IQ.\n";
else cout << "That's not an option, man. Pick again.";
} while(mystring != "adventurer" && mystring != "dreamer" && mystring != "politician");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// BETTER
bool valid;  // variable to track whether the input was valid or not
do
{
    valid = true;  // assume input is going to be valid
    cout << "Are you an\n"
            "adventurer, politician, or dreamer? ";
            
    string mystring;
    cin >> mystring;
    
    if( mystring == "adventurer" )
        cout << "That's far out, man. You have +2 in all attributes.\n";
    else if( mystring == "dreamer" )
        cout << "That's dreamy, man. You have +5 in skill.\n";
    else if( mystring == "politician")
        cout << "That's psychadelic, man. You have +5 in IQ.\n";
    else
    {
        cout << "That's not an option, man. Pick again.";
        valid = false;   // input was not valid
    }
}while(!valid);         // keep looping until input was valid 
Last edited on
Topic archived. No new replies allowed.