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:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <stdlib.h>
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?"<<endl;
	cout <<"Name:";
	cin >> Name;
	cout << "Hello, "<<Name <<". This is a program that has multiple functions, heres a list:" <<endl <<endl;
	cout << "1 Multiply numbers" <<endl;
	 cout << "2 Add numbers" <<endl;
	  cout << "3 Subract numbers" <<endl;
	   cout << "4 Divide numbers" <<endl;
	    cout << "5 Change name" <<endl;
	     cout << "6 Exit program" <<endl;
	    cin >> 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 " <<Mproduct;
	    
add:
		cout << "Enter two numbers you'd like to Add "<<endl;
		cout << "First Number:";
		cin >> Anumb1;
		cout << "Second Number:";
		cin >> Anumb2;
		  Aproduct = Anumb1+Anumb2;
		      cout << "The answer is "<<Aproduct;
	
	 


	system("pause");
	return 0;

 
}

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.

1
2
3
4
5
6
7
8
9
10
11
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.