I need to build a simple integer caluclator that perform arthmetic functions of addition, subtraction, multiplication, modulus and disvision. The calculator contains an Accumulator that stores the current result. The Accumulator is also involved with each operation. Make sure that you define a class for this program.
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 (Acc op= number)
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 following characters are legal operations:
+, -, *, %, /, =, and !.
The ! is treated the same as =, but causes the program to terminate. Any other characters entered as operations should cause a brief error message and should be ignored. Division by zero errors cause a reset.
Sample run:
SuperCalculator at you service!
------
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
------
SuperCalculator Off!
Now I have worked and worked on this and I can not get my calculator to work. Can you see the error of my ways. Please help me find my way to the solution?
#include "stdafx.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
class SupCal
{
public:
// function that displays welcome message
void displayMessage()
{
cout << "Super Calculator At Your Service!" << endl;
} // end function displayMessage
void displaySignoff()
{
cout << "Super Calculator off" << endl;
} //end funtion displaySignoff
}; // end class SupCal
int main()
{
int acc = 0; // accumulator is set to zero
int input; // number input
char oper='+'; // operation is set to '+'
SupCal mySupCal; // create a SupCal oject
mySupCal.displayMessage(); // call object's displayMessage function
while(oper!= '!')
cout << "Enter Number: "; // Prompt user to enter number
cin >> input; // accept number
switch (oper)
{
case '+':
acc += input;
break;
case '-':
acc -= input;
break;
case '*':
acc*=input;
break;
case '/':
if(input==0)
{
cout << "Division by zero - start over\n" << endl;
acc=0;
oper='+';
}
else
acc/=input;
break;
case '%':
if(input == 0)
{
cout << "Division by zero - start over\n" << endl;
acc=0;
oper='+';
}
else
acc %= input;
break;
default:
acc+=input;
break;
}
while(1)
{
cout << "Operation: ";
cin >> oper;
if(oper!='+'&& oper!='-' && oper!='*'&& oper!='/'&& oper!='%'&& oper!='='&& oper!='!')
{
cout<<"\nInvalid Operation - ignored";
}
else
break;
}
if(oper=='=' || oper=='!')
{
cout<<"Result: "<< acc << endl;
acc=0;
oper='+';
if(oper=='!')
{
mySupCal.displaySignoff(); // call object's displayMessage function
}
}
return 0;
} // end main
The programs never stops running.
Thanks,
M~