Can somebody help me with choosing an option?

Hello everyone, i am a complete noob in c++, i am trying to make a calculator but i am confused with letting the user input an option ex. 1 or 2; if anybody help me with this it be great.
-Again thanks in advance.
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
/* Building the Calculator */

#include <iostream>
#include <string>

using namespace std;

int main(){
		
		int user;
		string user1;
		int input;

		/* Welcoming the user */

		cout << "Welcome!" << endl;
		cout << "################################" << endl;
		cout << "Press any key to begin" << endl;
		cin >> user;
			
		/* Starting the calculator */
		int 1,2,3,4;
		x = a;
		y = b;
		1 = a + b;
		2 = a - b;
		3 = a * b;
		4 = a / b;


		cout << "What would you like to do?" << endl;
		cout << "Please choose an option 1-4" << endl;
		cout << "1) Add 2) Subtract 3) Multiply 4) Divide" << endl;
		cin >> input;
		if (input == '1'){
			cout << "What would you like to add?" << endl;
			cin >> a >> b;
			cout << a << "+" << b << "=" << a+b << endl;
		}
		else (input == '2'){
			cout << "What would you like to subtract?" << endl;
			cin >> a >> b;
			cout << a << "-" << b << "=" << a-b << endl;
		}
		else if (input == '3'){
			cout << "What would you like to multiply?" << endl;
			cin >> a >> b;
			cout << a << "*" << b << "=" << a*b << endl;
		}
		else (input == '4'){
			cout << "What would you like to divide?" << endl;
			cin >> a >> b;
			cout << a << "/" << b << "=" << a/b << endl;
		}
	system("pause");		
	return 0;
}
Last edited on
shouldnt else (input == '2'){ this statement be else if? Only the last one should be else.
the lines 22,25,26,27,28 are invalid
x and y was not declared.
No, that's not how you declare variables in C++ as0re, you need an identifier, an identifier is simply a name, an identifier can't be numbers as you did in line 22, also in line 40, you can't use else followed by else if, here i completely modified your program:

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
58
59
60
61
62
63
64
#include <iostream>
#include <string>

using namespace std;

int main(){
		
		int user;
		string user1;
		int input;
		char choose;
		/* Welcoming the user */

		cout << "Welcome! \n" << endl;
			
		/* Starting the calculator */
		int a = 0,b = 0,c = 0,d = 0;

		cout << "What would you like to do?" << endl;
		cout << "Please choose a valid option: \n"<<endl;
		cout << "a) Addition\n";
		cout << "s) subtraction\n";
		cout << "d) division\n";
		cout << "m) multiplication\n";
		cin >> choose;
		if (choose == 'a'){
			cout << "What would you like to add?" << endl;
			cout<<"Enter first number: \n";
			cin>>a;
			cout<<"Enter second number: \n";
			cin>>b;
			cout << a << "+" << b << "=" << a+b << endl;
		}
		else if (choose == 's'){
			cout << "What would you like to subtract?" << endl;
			cout<<"Enter first number: \n";
			cin>>a;
			cout<<"Enter second number: \n";
			cin>>b;
			cout << a << "-" << b << "=" << a-b << endl;
		}
		else if (choose == 'm'){
			cout << "What would you like to multiply?" << endl;
			cout<<"Enter first number: \n";
			cin>>a;
			cout<<"Enter second number: \n";
			cin>>b;
			cout << a << "*" << b << "=" << a*b << endl;
		}
		else if (choose == 'd'){
			cout << "What would you like to divide?" << endl;
			cout<<"Enter first number: \n";
			cin>>a;
			cout<<"Enter second number: \n";
			cin>>b;
			cout << a << "/" << b << "=" << a/b << endl;
		}
		else
		{
			cout<<"Invalid input\n";
		}
	system("pause");		
	return 0;
}
so in lines 40,45 should be else if and line 50 just else?
also im confused with the x and y not being declared, how do i declare them?
Thank you Uk Marine this has solved my problem.
Last edited on
Topic archived. No new replies allowed.