Thread: Need some help with Calculator program

1. Need some help with Calculator program

Here a demo of the program. I trying to figure out how to make it take more then 2 number without added more variable. I know you have to use some kind of loop just having a little bit of trouble figuring it out.... THANKS.....

Code:
```#include <iostream>
#include <cmath>
#include <stdio.h>
#include <windows.h>

using namespace std;

void ErrorMessage();

int main() {

float FirstNumber;
float SecNumber;
char Sign;
float Equal;

cout << endl;
cout << "\t\t\t\tEnter Math Problem.\n";

while ( true ) {

cout << ">";
cin >> FirstNumber >> Sign >> SecNumber;

switch ( Sign ) {

case '+':

Equal = ( FirstNumber + SecNumber );

cout << endl;

cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";

cout << endl;

break;

case '-':

Equal = ( FirstNumber - SecNumber );

cout << endl;

cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";

cout << endl;

break;

case '*':

Equal = ( FirstNumber * SecNumber );

cout << endl;

cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";

cout << endl;

break;

case '/':

if  ( ( FirstNumber == 0 ) || ( SecNumber == 0 ) ) {

ErrorMessage();

continue;

} else {

Equal = ( FirstNumber / SecNumber );

cout << endl;

cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";

cout << endl;

break;
}

default:

ErrorMessage();

cout << "You can't do that!\n\n";

continue;

}

}

system("pause");

return(0);

}

void ErrorMessage() {

cout << endl;

cout << "ERROR!!! \n";

cout << endl;

}```

2. Why is FirstNumber equal to zero on divide an error? It's a valid operation.

3. Well if you were doing subtraction for instance,

a - b - c

you would first subtract b from a and then finally c from the difference. So your loop control would be how many terms there is going to be. In this case, there are two, as you would read a and b and then c.

It may be helpful to use a sentinel value like zero, to signal that the last term is complete.

4. If you have 1 + 2 * 3 that won't work. It is more complicated. If not, if you only + and - or * and / then you will have:
firstNumber operator OtherNumber operator OtherNumber .... OtherNumber = Result

So you read the first number, save it in TempResult and then in a loop you do: