- 3

Please make this C++ code repetitve to use over and over (please answer urgently)

Help to repeat C++ Calculator in my C++ code post --Help Me Out. #include<iostream> using namespace std; int main() {     int ans;     int a;     int b;     int c;     int d;     int e;     int f;     int g;     int add;     int sub;     int mult;     int div;   cout<<"Use Any Function \n Type-1 for addition: \n Type-2 for subtraction: \n Type-3 for multiplication: \n Type-4 for division: ";     cin>>ans;      if(ans==1)      {                    cout<<"Okay first number to add:";         cin>>a;         cout<<"Write second number:";         cin>>b;         add=a+b;         cout<<"Your answer is:"<<add;       }        if (ans==2)           {              cout<<"Type number:";         cin>>c;         cout<<"number subtracted by:";         cin>>b;         sub=c-b;         cout<<"Your answer is:"<<sub;       }       if(ans==3)        {        cout<<"Type number to multiply:";        cin>>d;        cout<<"Multiplied by:";        cin>>e;        mult=d*e;        cout<<"Your answer is:"<<mult;       }       i

10th Jan 2021, 4:20 PM
Code.Twister
Code.Twister - avatar
4 Answers
+ 5
Joel -Mr.Twister , to give you a start, here is how the code could begin. all the rest is up to you and your turn... int num1, num2, ans; double res; cout << "Use Any Function \n Type-1 for add: \n Type-2 for sub: \n Type-3 for mul: \n Type-4 for div: \n"; cin >> ans; cout << "Okay first number: " << endl; cin >> num1; cout << "Write second number: " << endl; cin >> num2; if(ans==1){ res = num1 + num2; } ....... cout<<"Your answer is: " << res ;
10th Jan 2021, 6:11 PM
Lothar
Lothar - avatar
+ 9
What kind of urgency is there ? and also define what do you mean by "repetitive to use" do you mean making it reusable or making it loop till user doesn't want quit ? your question is unclear
10th Jan 2021, 4:24 PM
Arsenic
Arsenic - avatar
+ 7
Joel -Mr.Twister , before proceeding with your code, you should think about avoiding repeated code. you are using a whole bunch of variables, different ones for different operations, but all have the same data type. from my point of view, you need not more than 4 variables: - numerical input 1 - numerical input 2 - operator to use - result also you don't need to have multiple input routines, you only need to have 2 for the both numerical values and one for the operator. Applying this to your code will greatly reduce the size of your program. Happy coding!
10th Jan 2021, 5:33 PM
Lothar
Lothar - avatar
+ 6
Joel -Mr.Twister Add a do while loop .
10th Jan 2021, 4:39 PM
Alphin K Sajan
Alphin K Sajan - avatar