### Simple calculator operation

Hey guys so I'm trying to modify my old calculator program from previous assignments, into a calculator made of functions. What I'm struggling with is trying to make the calculator add the previous answer instead of the first number I put in. Thanks for helping!

 ``12345678910`` ``````#ifndef header #define header int Divide (int, int); int Add (int, int); int Mult (int, int); int Sub (int, int); #endif ``````

 ``1234567891011121314151617181920`` ``````int Add (int X, int Y) { return X + Y; } int Mult (int X, int Y) { return X * Y; } int Sub (int X, int Y) { return X - Y; } int Divide (int X, int Y) { return (X / Y); } ``````

Main
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include using namespace std; #include #include "header.h" void main () { double X; double Y; double Z; char op; bool vop; X = 0; Y = 0; Z = 0; vop = true; begin: cout << "Enter a whole number: "; cin >> X; Z = X; do { cout << "Enter operator (+-*/CcXx): "; cin >> op; if ((op != 'c' && 'C') && (op != 'X' && 'x')) switch (op) { case '+': cout << "Enter another number: "; cin >> Y; Z = Add (X,Y); cout << "Total: " << Z << endl; vop = true; break; case '-': cout << "Enter another number: "; cin >> Y; Z = Sub (X,Y); cout << "Total: " << Z << endl; vop = true; break; case '*': cout << "Enter another number: "; cin >> Y; Z = Mult (X,Y); cout << "Total: " << Z << endl; vop = true; break; case '/': cout << "Enter another number: "; cin >> Y; Z = Divide (X,Y); cout << "Total: " << Z << endl; vop = true; break; case 'C': case 'c': cout << "Cleared" << endl; Z = 0; vop = true; goto begin; case 'X': case 'x': cout << "Bye"<< endl; exit (0); default: vop = false; cout << "Enter another operator" <
Last edited on
closed account (Dy7SLyTq)
http://www.stroustrup.com/Programming/Solutions/Ch7/e7-1.cpp
just thought you might want to see that for reference
I don't really understand the logic in some parts of your program:
- please explain what line 32 is supposed to achieve. If I choose c, the switch is not executed, but it is executed if I type C
- if you don't want to execute for c, why do you still treat the c case on line 69
- if you enter operator v, it will go to default, vop is false, prints "Enter another operator" then exits at line 87
- I REALLY hate goto statements. There might be some place for them, but you should avoid them. It makes code much harder to read

As for your question, if you really want to apply operator on the previous result, you need to replace Add(X,Y) with Add (Z,Y) and so on for Sub, Mult, and Divide
That site might be a little too complicated for me to understand sadly...
Thought I had to put line 32 in there.

Don't know why c isn't working...

Just noticed that problem too, going to try and fix that

I've been trying to figure out how to not use goto, but can't find a way to do that so far.

Thank you!
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````int main() { double x=0., y=0., z=0.; char op; bool reset=true; do { if (reset) { cout << "Enter a number: "; cin >> x; reset=false; } else x=z; cout << "Enter operator (+-*/CcXx): "; cin >> op; switch (op) { case '+': cout << "Enter another number: "; cin >> y; z = Add (x,y); cout << "Total: " << z << endl; break; case '-': cout << "Enter another number: "; cin >> y; z = Sub (x,y); cout << "Total: " << z << endl; break; case '*': cout << "Enter another number: "; cin >> y; z = Mult (x,y); cout << "Total: " << z << endl; break; case '/': cout << "Enter another number: "; cin >> y; z = Div (x,y); cout << "Total: " << z << endl; break; case 'C': case 'c': cout << "Cleared" << endl; reset=true; break; case 'X': case 'x': op='x'; break; default: cout << "Invalid choice. Enter another operator" <

I did not test it, so you might find some errors.
Last edited on
Topic archived. No new replies allowed.