I am trying to make a simple calculator program, where I ask the user if they would like to add, subtract, multiply, or divide. But no matter what they choose it prints all four of the answers. Here is the code.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int answer;
int sum;
sum = a + b;
int difference;
difference = a - b;
int product;
product = a * b;
int quotient;
quotient = a / b;
cout << "Type a number! \n";
cin >> a;
cout << "Type another number! \n";
cin >> b;
cout << "Would you like to add, if so type 1, subtract, if so type 2, multiply, if so type 3, or divide, if so type 4.";
cin >> answer;
if(answer == 1)
{
cout << "Okay \n";
cout << "The sum of your numbers is " << sum << endl;
}
else if(answer == 2)
{
cout << "Okay \n";
cout << "The difference of your numbers is " << difference << endl;
}
else if(answer == 3)
{
cout << "Okay \n";
cout << "The product of your numbers is " << product << endl;
}
else if(answer == 4)
{
cout << "Okay \n";
cout << "The quotient of your numbers is " << quotient << endl;
}
else
{
cout << "You appear to have not typed a number 1 - 4, please try again.";
cin >> answer;
}
This program is very simple hahaha, The thing that may be a bit confusing for beginners are the functions. but you can just put this all in main if you would like
/* This is a really simple calculator you're more then
* welcome to use this or simply build off of it, it may look complicated
* but it really isn't!
*/
#include <iostream>
usingnamespace std;
//global variables. in this case I think it's easier if I do it this way it should seem more clear to ya.
int input;
int NumberOne;
int NumberTwo;
//The following are the operational functions called in the switch statement
int addition(int a, int b)
{
int sum;
sum=a+b;
return sum;
}
int subtraction(int a,int b)
{
int sum;
sum=a-b;
return sum;
}
int multiplication(int a,int b)
{
int sum;
sum=a*b;
return sum;
}
int division(int a,int b)
{
int sum;
sum=a/b;
return sum;
}
int main()
{
cout << " 1 - addition:\n 2 - subtraction:\n 3 - multiplication:\n 4 - division: " << endl;
cin>>input;
switch (input)
{
case 1: cout << "Enter First Number";
cin >> NumberOne;
cout << " Enter Second Number";
cin >> NumberTwo;
cout <<NumberOne << "+" << NumberTwo << "=" << addition(NumberOne,NumberTwo)<<endl;
break;
case 2: cout << "Enter First Number";
cin >> NumberOne;
cout << " Enter Second Number";
cin >> NumberTwo;
cout << NumberOne <<"-" << NumberTwo << "=" << subtraction(NumberOne,NumberTwo)<<endl;
break;
case 3: cout << "Enter First Number";
cin >> NumberOne;
cout << " Enter Second Number";
cin >> NumberTwo;
cout << NumberOne << "*" << NumberTwo << "=" << multiplication (NumberOne,NumberTwo)<<endl;
break;
case 4: cout << "Enter First Number";
cin >> NumberOne;
cout << " Enter Second Number";
cin >> NumberTwo;
cout << NumberOne << "/" << NumberTwo << "=" << division(NumberOne,NumberTwo) <<endl;
break;
}
return 0;
}
/*I was going to throw some loops into it but since you're looking for some thing simple I figured this might help you out*/
#include <iostream>
usingnamespace std;
int main()
{
int a;
int b;
int answer;
int sum;
int difference;
int product;
int quotient;
cout << "Type a number! \n";
cin >> a;
cout << "Type another number! \n";
cin >> b;
cout << "Would you like to add, if so type 1, subtract, if so type 2, multiply, if so type 3, or divide, if so type 4.";
cin >> answer;
if (answer == 1)
{
cout << "Okay \n";
sum = a + b;
cout << "The sum of your numbers is " << sum << endl;
}
elseif (answer == 2)
{
cout << "Okay \n";
difference = a - b;
cout << "The difference of your numbers is " << difference << endl;
}
elseif (answer == 3)
{
cout << "Okay \n";
product = a * b;
cout << "The product of your numbers is " << product << endl;
}
elseif (answer == 4)
{
cout << "Okay \n";
quotient = a / b;
cout << "The quotient of your numbers is " << quotient << endl;
}
else
{
cout << "You appear to have not typed a number 1 - 4, please try again.";
cin >> answer;
}
system("PAUSE");
return 0;
}
I moved the following 4 lines of code:
[code]
sum = a + b;
difference = a - b;
product = a * b;
quotient = a / b;
The reason you were having an issue was because you were trying to assign those 4 pieces of code a value before gathering the values of a and b. I hope this is clear enough for you to understand.