I learned how to create a calculator that adds numbers, but that is all it does. I want to create a calculator that can add, subtract, multiply, and divide, but I just don't know how to do that. Is there a code for that, or is it impossible?
Thank you.
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main()
{
int choice;
int number1, number2;
cout << "Choose your operation. \n ";
cout <<"1 = Addition \n" ;
cout <<"2 = Subtraction \n" ;
cout <<"3 = Multiplication \n" ;
cout <<"4 = Division \n ";
cout <<"0 = EXIT \n \n";
cout << "Choice : ";
cin >> choice;
switch(choice)
case 1:
//Addition
cout << "You are Adding. Enter a number you would like to add. \n Number 1: ";
cin >> number1;
cout << " Enter a second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: "<< number1 + number2 << endl;
cin.get() //This is used to pause the program
break;
case 2: //subtraction... ect.
break;
case 3: //multiply...
break;
case 4: //divide...
break;
}
This is just an example though. Im sure that there are many ways to clean it up, and if i missed anything i apologize. Case 2, 3 and 4 are basically the same as 1, just using different operators. You could also use a loop to keep the program going until you quit. Hope this helps.