Reputation: 1
Im learning C++ and I have a assigment to create small calculator using if,else if statements.The program should work like this.First you need to choose the operation like add or substract then enter first argument and then second argument and get a result.I write the code but it doesnt show the address right.The answers shows different result from what I expected.The multiple and divide give me the right result but add and substract wrong. Please help.
#include<iostream>
using namespace std;
int main()
{
int a = 1, b = 2, c = 3, d = 4;
int first_argument, second_argument;
cout << "Please choose the number: " <<endl<< "1.Multiple"<<endl<<"2.Divide" <<endl<<"3.Substract"<<endl<<"4.Add" << "\n";
cin >> a,b,c,d;
cout << "Please choose the first argument" << "\n";
cin >> first_argument;
cout << "Please choose the second argument" << "\n";
cin>>second_argument;
if (a==1)
cout <<"The answer is "<<first_argument*second_argument<< "\n";
else if (b == 2)
cout << "The answer is " << first_argument / second_argument << "\n";
else if (c == 3)
cout << "The answer is " << first_argument - second_argument << "\n";
else if (d == 4)
cout << "The answer is " << first_argument + second_argument << "\n";
cin.ignore();
cin.get();
return 0;
}
Upvotes: 0
Views: 121
Reputation: 56557
cin >> a,b,c,d;
should be
cin >> a >> b >> c >> d;
Otherwise you invoke the infamous comma operator, which has the lowest precedence, so your expression is translated as
(cin >> a), b, c, d;
which just reads a
then evaluates b
, c
and d
, and the end result of the expression is d
, which of course is not used later. But in any case, you don't need 4 variables for this task, only one is enough. You then need to test its value and depending on it perform the required operation.
Side note: when you divide integers, the result will be truncated. If you don't want this, then cast one of the integers to a double
, like
first_argument / static_cast<double>(second_argument)
Upvotes: 2
Reputation: 37834
cin >> a,b,c,d;
does not make any sense.
You most likely only want to read in the user's selected operation into a single variable, and use that value throughout your if else blocks.
example:
#include <iostream>
using namespace std;
int main() {
int operation, first_argument, second_argument;
cout << "Please choose the number: " << endl << "1.Multiple" << endl << "2.Divide" << endl << "3.Substract" << endl << "4.Add" << "\n";
cin >> operation;
cout << "Please choose the first argument"
<< "\n";
cin >> first_argument;
cout << "Please choose the second argument"
<< "\n";
cin >> second_argument;
if (operation == 1)
cout << "The answer is " << first_argument * second_argument << "\n";
else if (operation == 2)
cout << "The answer is " << first_argument / second_argument << "\n";
else if (operation == 3)
cout << "The answer is " << first_argument - second_argument << "\n";
else if (operation == 4)
cout << "The answer is " << first_argument + second_argument << "\n";
cin.ignore();
cin.get();
return 0;
}
Upvotes: 0