### 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````/* Building the Calculator */ #include #include 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:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include 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"<> 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.