### Program with multiple choices

Im currently working on a basic program that gives you 6 choices to choose from and you choose by inputting a number that corresponds with that selected choice. I've been running into more of a bug that chooses the first choice if the number you input is any number (between 2 and 6) except for 1, this isn't a problem if you choose the first choice but it is a problem if you want any other choice. Im sure you can understand what is going on in the code but for me im having a bit of trouble trying to figure out a way to get around this problem.
Heres an example:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include #include using namespace std; int main () { int a; int Mnumb1; int Mnumb2; int Mproduct; int Anumb1; int Anumb2; int Aproduct; string Name; cout <<"What is your name?"<> Name; cout << "Hello, "<> a; if (a == 1) goto subtract; else subtract: cout << "Enter two numbers to multiply "<< endl; cout << "First Number:"; cin >> Mnumb1; cout << "Second Number:"; cin >> Mnumb2; Mproduct = Mnumb1*Mnumb2; cout << "The answer is " <> Anumb1; cout << "Second Number:"; cin >> Anumb2; Aproduct = Anumb1+Anumb2; cout << "The answer is "<

You really shouldn't be using goto in 99.9% of cases. It can make it very hard to keep up with how a program is flowing.

I suggest you use the standard methods which are either if/else statements or a switch statement.

 ``1234567891011`` ``````if(a == 1) { // Do stuff } else if(a == 2) { //Do stuff } etc``````
Thanks it's working now ;)
The only reason i was using goto was because we are currently going over goto statements so i wanted to implement it in the program but the if and if else statements should do just fine :D
closed account (3qX21hU5)
Or you can use a switch case which would probably work better in this situation.

 ``1234567891011121314`` ``````switch (input) { case '1': // Do stuff break; case '2': // Do stuff for number 2 break; default: // Do stuff for every other input other then the ones specified break; }``````

Basically how it works is if the user enter's lets say 1 it will run whatever code is in the `case '1':`. And the default case is used for when someone enter a input that you have not specified like lets say they entered 3, since I don't have a case for 3 it will run the default case and whatever code is in that.
Alright i'll try out the switch statement in a little 'exercise' program tomorrow, haven't really covered much in class and i just feel like the learning is too slow so it's great to try out new things, i appreciate it.
Topic archived. No new replies allowed.