### how can i improve this

how can i improve this calculator?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192`` ``````#include #include using namespace std; int Amount; int Number; int whatoperation; int restart; class operations { public: int add() { int Total = 0; cout << "Enter in how many numbers you want to use" << endl; cin >> Amount; for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount { cout << "Number " << i << ": " ; // Shows user what number they are entering cin >> Number; Total = Number + Total; // Adds number to total } cout << "The total is " << Total << endl; // Prints out total value when loop has finished Total = 0; // resets total back to zero } int subtract() { int Total = 0; cout << "Enter in how many numbers you want to use" << endl; cin >> Amount; for(int i = 0; i < Amount; i++) { cout << "Number " << i << ": " ; cin >> Number; if(i == 0) { Total = Total + Number; } else { Total = Total - Number; } } cout << "The total is " << Total << endl; Total = 0; } int multiply() { int Total = 0; cout << "Enter in how many numbers you want to use" << endl; cin >> Amount; for(int i = 0; i < Amount; i++) { cout << "Number " << i << ": " ; cin >> Number; if(i == 0) { Total = Total + Number; } else { Total = Total * Number; } } cout << "The answer is " << Total << endl; Total = 0; } int divide() { int Total = 0; cout << "Enter in how many numbers you want to use: " << endl; cin >> Amount; for(int i = 0; i < Amount; i++) { cout << "Number " << i << ": " ; cin >> Number; if(i == 0) { Total = Total + Number; } else { Total = Total / Number; } } cout << "The total is " << Total << endl; Total = 0; } int average() { int Total = 0; cout << "Enter how many numbers you want to use: "; cin >> Amount; for(int i = 0; i < Amount; i++) { cout << "Number " << i << ": "; cin >> Number; Total = Total + Number; } Total = Total / Amount; cout << "The average is " << Total << endl; Total = 0; } }; int main() { operations callfunctions; int Total = 0; int Amount; int Number; int whatoperation; int restart; do{ system("CLS"); cout << "Here are your choices(enter the number before the operation)" << endl << endl; cout << "1.Addition" << endl; cout << "2.Subtraction" << endl; cout << "3.Multiplication" << endl; cout << "4.Division" << endl; cout << "5.Average Calculator" << endl; cin >> whatoperation; system("CLS"); switch(whatoperation){ case 1: callfunctions.add(); break; case 2: callfunctions.subtract(); break; case 3: callfunctions.multiply(); break; case 4: callfunctions.divide(); break; case 5: callfunctions.average(); break; } cout << "Enter 1 to restart or 2 to quit: "; cin >> restart; }while(restart == 1); cin.get(); return 0; }``````

- Remove global variables; they are completely unnecessary.
- There is no need for the class called 'operations'.
- Give the user something user friendly, like '5 + 5 + 5 = (display total)'.

Those are just a few minor complaints. You could really simplify this and cut about 130 lines off of it (maybe even more). Focus on writing applications that your dog would be able to use and understand. Typing something like 5 * 5 is much better than having to select from a menu then enter the amount of number you would like to add.

I would usually complain about calling system `system("CLS")`, but I have found myself doing it before so I'll cut you some slack. Just remember, calling system leaves a huge security hole in your program, is very resourceful, and leaves your program 'grounded' to a specific OS.
Last edited on
how would i make it so they dont have to enter how many numbers they want to use
Make it so you can type:
 9(1 + 6)

and the total is 63
Last edited on
how can i do that. do you know a tutorial?
?
Essentially, to be able to make calculations based on something like this:
 9(1 + 6)

you'll have to parse the user's input to basically separate the numbers from the operators and translate them into something that your program can calculate an output for. You'll want to find tutorials about parsing/splitting strings for this. The amount of work for this really depends on how interpretive of human typing you want your calculator to be. Like, are these: 9 (1 + 6) and 9(1+6) valid inputs ? or will only this one work: 9*(1+6) ?

you would start with accepting a full string as an input. Try it with the simplest case first like accepting only `2+3` from from the user into a string and then parse it and calculate then print the output " = 5".