# Help w/ program

• 02-24-2003
Newbie96
Help w/ program
I am attempting to create a simple integer calculator that supports addition, subtraction, multiplication, modulus, and division.
The calculator contains an Accumulator that stores the current result.
Once started, the Accumulator is set to zero, and the operation to be performed is set to addition (+). The program then repeats the following steps:

Request and Accept a number
Apply the stored operation )
Request and Accept the next operation
This process repeats until the equals sign (=) is entered as an operation. At this point, the Accumulator contents are displayed and the calculator is reset (Accumulator set to zero and operation to +).

The ! is treated the same as =, but causes the program to terminate.
Any other characters entered as operations should cause a brief error message

It suppose to run like this:
------
Input: 10
Operation: *
Input: 2
Operation +
Input: -5
Operation: =
Result: 15
------
Input: 12
Operation: /
Input: 0
**Division by zero - start over
------
Input: 12
Operation: >
**Invalid operation - ignored
Operation: /
Input: 5
Operation: =
Result: 2
------
Input: 10
Operation: %
Input: 6
Operation: *
Input: 3
Operation: !
Result: 12
------

I can't get the program to work correctly. Any help would be greatly appreciated.

the following is my code:

Code:

```#include <iostream.h> int main() {         int value=0;         char oper_and;     // input the loop count     int accumulator = 0;             cout << "Welcome to CalcWiz\n";     cout << "Enter number: ";         cin>>value;         accumulator +=value;                     // loop "forever"     while (oper_and != '!')     {         // fetch a number                 cout << "Enter operand:";                 cin>>oper_and;                       if (oper_and == '!')         {             // ...then exit             break;         }                 cout << "Enter number:";                 cin>>value;                 // ...otherwise add the number to the         // accumulator                 switch (oper_and)                 {                         case '+':                                 accumulator += value;                         case '-':                                 accumulator -= value;                         case '*':                                 accumulator *= value;                         case '/':                                 accumulator /= value;                         case '%':                                 accumulator %= value;                         if (value == 0) {                 cout << "Error: Divide by zero\n";                 cout << "  operation ignored\n";             } else                       accumulator /= value;                         case '=':                                 cout<<"The result is:"<<accumulator;                                 accumulator = 0;                         default:                                 cout<<"\nDIDN'T RECOGNIZE THAT!"<<endl;                 }     }       // output the accumulated result     cout << "\nThe total is "         << accumulator         << "\n";     return 0; }```
• 02-24-2003
Magos
What is not working. Can you be a little more specific on what errors you get.
One thing I noted was that the check for division by 0 should be in the division case too.
And you don't have any breaks in your switch.
• 02-24-2003
Newbie96
revised-plz help
My error comes, when I input =.
1) I don't get a result, it ask me for another another number, than displays results.
2) My accumulator does not reset, so my numbers keep accumulating.

thanx for the help!

this is the revised code:

[code]
// loop "forever"
while (oper_and != '!')
{
// fetch a number
cout << "Enter operand:";
cin>>oper_and;

if (oper_and == '!')
{
// ...then exit
break;
}

cout << "Enter number:";
cin>>value;

// ...otherwise add the number to the
// accumulator
switch (oper_and)
{
case '+':
accumulator += value;
break;
case '-':
accumulator -= value;
break;
case '*':
accumulator *= value;
break;
case '/':
accumulator /= value;
if (value == 0) {
cout << "Error: Divide by zero\n";
cout << " operation ignored\n";
} else
accumulator /= value;
break;
case '%':
accumulator %= value;
break;

case '=':
cout<<"The result is:"<<accumulator;
accumulator = 0;
break;
default:
cout<<"\nDIDN'T RECOGNIZE THAT!"<<endl;
}
}

// output the accumulated result
cout << "\nThe total is "
<< accumulator
<< "\n";

return 0;
}
[\code]
• 02-24-2003
Magos
Quote:

Code:

```// fetch a number cout << "Enter operand: "; cin>>oper_and; if(oper_and == '!') {   // ...then exit   break; } cout << "Enter number:"; cin>>value;```

I see no reason why it shouldn't ask for a number considering those two input statements follow each other...
If you don't want it to ask for a number, use an if(oper_and != '=') around the number input.

PS: Why oper_and instead of operand?