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; }



LinkBack URL
About LinkBacks


