0

Please find error? I am trying to make simple calculator...I have learned up to if statement and based on that knowledge I made

#include <iostream> using namespace std; int main(int argc, char *argv[]) { int a,b,sum,diff,multi,div; cout<<"enter first no."<<endl; cin>>a; cout<<"enter second no."<<endl; cin>>b; cout<<"what do you want to do?"<<"sum "<<"diff "<<"multi "<<"div "<<endl; cin>>sum>>diff>>multi>>div; if (cin>>sum) { sum=a+b; } if (cin>>diff) { diff=a-b; } if (cin>>multi) { multi=a*b; } if (cin>>div) { div=a/b; } return 0; }

2nd Oct 2021, 2:37 PM
Kunj Pandya
Kunj Pandya - avatar
9 Respostas
+ 4
1. so many useless variables you created when you need only 3, i.e a, b and c(use data type string) 2. instead of <<"sum "<<"diff "<<"multi "<<"div"<<endl; you can do this cout << "what do you want to do?/n" << "sum diff multi div" << endl; to make it easier to read. 3. you don't need to ask so many inputs, just ask for input in the variable c(make sure you declared it) is enough. 4. instead of cin, use c variable in the conditions 5. instead of left shifting the bits for no reason, use comparison == , in all your if conditions 6. you cannot compare uninitialized variables, so you make them strings by surrounding sum, diff, multi and div by " 7. you can just directly print them out, instead using another variable to assign a and b variable, for eg-: cout << a - b; do this to all of your if statements 8. instead of using if-if statement, use if-else statement instead.
2nd Oct 2021, 2:55 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 1
Kunj Pandya did you try running @Eashan Morajkar's code? it looks and works fine by me expect what I said in 2nd point
2nd Oct 2021, 2:59 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
0
#include <iostream> using namespace std; int main(int argc, char *argv[]) { int a,b; double total; string way; cout<<"enter first no: "<<endl; cin>>a; cout<<"enter second no: "<<endl; cin>>b; cout<<"what do you want to do?"<<" sum "<<",diff "<<",multi "<<",div: "; cin >> way; if (way=="sum") { total=a+b; } else if (way=="diff") { total=a-b; } else if (way=="multi") { total=a*b; } else if (way=="div") { total=a/b; } return 0; }
2nd Oct 2021, 2:45 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
In every if..else if statement you're taking an input
2nd Oct 2021, 2:46 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
Still not working bro..
2nd Oct 2021, 2:50 PM
Kunj Pandya
Kunj Pandya - avatar
0
It works
2nd Oct 2021, 2:58 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
...we already know about that, judging by your code
2nd Oct 2021, 3:02 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
0
I run Eashan Morajkar code but Rellot's screwdriver it's run same as mine
2nd Oct 2021, 3:03 PM
Kunj Pandya
Kunj Pandya - avatar
0
Thank you but I am a learner actually so I learn from those mistakes 😅Rellot's screwdriver
2nd Oct 2021, 3:03 PM
Kunj Pandya
Kunj Pandya - avatar