My calculator is complete but I realized my teacher asked for this one last thing.
‘After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation.’
This is my code so far. I just want to know how to do this so this is no longer a problem in the future. (I want the program to be to keep adding numbers until x is pressed)
#include <iostream> // iostream #include <ctype.h> // #include ctype.h use this if using isdigit in the code below using namespace std; int main () { char Digit; int Answer; char Op; bool ValidNum; bool ValidOp; StartCalc: int Num1 = 0; int Num2 = 0; cout << "This program will store 2 numbers and calculate the result." << endl; // Enter a number for (int i = 0; i < 2; ++i) { int temp; do { cout << "Enter a number and then press ENTER: "; temp = 0; // this is set to 0 inside the loop so if the loop messes up then it resets again ValidNum = true; Digit = cin.get (); while (Digit != '\n') // while digit does not equal a enter button { if (isdigit (Digit)) // is that character a digit? // isdigit tests if the character is a certain type of character temp = (temp * 10) + (Digit - '0'); // digit in this case is the ones place if you were to enter a number bigger than 9 // so if input of 1 then ((0) * 10) + (('1') - '0') // if input of 21 ((0) * 10) + (('2') - '0'); (first) this makes it 2. // then ((2) * 10) + (('1') - '0'); now its 21 // third, wait there is a third character, that third character is newline aka enter. // that newline (\n) or third character is what breaks the while loop. else ValidNum = false; Digit = cin.get (); } if (!ValidNum) cout << "Not valid, try again" << endl; } while (!ValidNum); if (i == 0) { Num1 = temp; // stores temp as num1 cout << "Finished with number " << Num1 << endl; } if (i == 1) { Num2 = temp; // when running the second time it stores temp as num2 cout << "Finished with number " << Num2 << endl; } // this is baiscally array but not one, since i cant use arrays in this project I compromised. } // Enter an operator + - * / C X // if it is not a valid operator // tell the user and ask for another one do { cout << "Please input a operator (+, -, *, /, C, X) and press Enter: "; Op = cin.get(); // this gets the op switch (Op) { case '+': Answer = Num1 + Num2; break; case '-': Answer = Num1 - Num2; break; case '*': Answer = Num1 * Num2; break; case '/': Answer = Num1 / Num2; break; case 'c': // dont type break, this will allow c and C to have the same command // try using system("CLS"); case 'C': cout << "Clearing the calculator " << endl; cin.get(); goto StartCalc;// that cin.get accounts for the enter button since cin.get acounts for any input, without it the loop will start over and account the enter that is pressed. this replaces the first input to a zero rather and skips to ask for the second number.// goto reroutes the program to start from the label again, which is startcalc in this case. // when the user types c, the program is supposed to start from the beginning again until porgram is terminated with x. case 'x': case 'X': cout << "Terminating Program" << endl; exit(0); ValidOp = true; // my teacher set this up, idk why we would use this instead of exit break; default: ValidOp = false; // i use this to clear the calc, what does false do cout << "Not valid, try again, please enter +, -, *, /, 'c' or 'x' as operator. " << endl; break; } cout << "The result is: " << Answer << endl; NewA = Answer; } while (!ValidOp); return 0; }