+ 1

Problem with C++

----------------------------------------------------------------------------------------------------------------------- I am having a problem with a calculator that I'm developing. Can anyone tell me why wont it work properly? It actually doesn't give any error message, but the result doesn't add up. ----------------------------------------------------------------------------------------------------------------------- #include <iostream> using namespace std; int main() { /*PROJECT_CALCULATOR needs 3 inputs and is able to calculate 4 main things (xD i couldn't find the right term): addition, difference, product and quotient. You hate to send three imputs: the first can be SUM for addition, PRO for product, DIF for difference and QUO for quotient. The next 2 inputs will be the number who will be processed.*/ int SUM; int PRO; int DIF; int QUO; int operation; int X; int Y; int result; cin>>operation; cin>>X; cin>>Y; if (operation = SUM){ cout<< X+Y; }else if (operation = PRO){ cout<< X*Y; }else if (operation = DIF){ cout<< X-Y; }else if (operation = QUO){ cout<< X/Y; } return 0;}

4th Oct 2017, 4:52 PM
Simone Batistini
Simone Batistini - avatar
6 Answers
+ 4
u didn't mention the result in your program which u had declared as int. you need to use result for example result=x+y. note: try to take result in float it will help in division. and print result in end so that no use of cout more and more. hope you get your answer. and there should be == in if condition.
4th Oct 2017, 5:05 PM
Sudhanshu
+ 3
if condition it should have double == And if u are taking int for operation then take only one variable(operation) and display massage to user that 1 for add, 2 for subtract Or Take char for operation and values will be + - / * u can use switch case too
4th Oct 2017, 5:06 PM
Nitesh Kuwalekar
Nitesh Kuwalekar - avatar
+ 1
Your program have a lot of Errors and I rewrote it clearly.. Try to understand where the problem reside and if you can't understand just reply to me ................... #include <iostream> using namespace std; int main() { char S, P, D, Q, operation; float X, Y; cin>>operation; cin>>X; cin>>Y; if (operation == 'S') { cout<< X+Y; } else if (operation == 'P') { cout<< X*Y; } else if (operation == 'D') { cout<< X-Y; } else if (operation == 'Q') { cout<< X/Y; } return 0; }
4th Oct 2017, 5:30 PM
Fahem
Fahem - avatar
0
You should assign the values ​​to choose the type of operation that your calculator should perform and from there everything was fine: ------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; #include<cstring> int main() { /*PROJECT_CALCULATOR needs 3 inputs and is able to calculate 4 main things (xD i couldn't find the right term): addition, difference, product and quotient. You hate to send three imputs: the first can be SUM for addition, PRO for product, DIF for difference and QUO for quotient. The next 2 inputs will be the number who will be processed.*/ string SUM = "SUM"; string PRO = "PRO"; string DIF = "DIF"; string QUO = "QUO"; string operation; int X; int Y; int result; cin>>operation; cin>>X; cin>>Y; if (operation == SUM){ cout<< X+Y; }else if (operation == PRO){ cout<< X*Y; }else if (operation == DIF){ cout<< X-Y; }else if (operation == QUO){ cout<< X/Y; } return 0; }
4th Oct 2017, 5:47 PM
Janier LĂłpez Morales
0
Thank you guys
8th Oct 2017, 12:33 PM
Simone Batistini
Simone Batistini - avatar
0
thanks guys imma try later
14th Mar 2018, 7:08 AM
Simone Batistini
Simone Batistini - avatar