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.
/* Building the Calculator */
#include <iostream>
#include <string>
usingnamespace 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;
}
elseif (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;
}
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:
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.