Continuous Calculator w/ object class

#include <iostream>
using namespace std;

//1.This is my current code for a calculator,
//I need to make it continuous until the user press =.
//2. How do I change the functions of this calculator into
//something that embeds into an object class?

double number1, number2;
double result;
char operation;

void main (void)
{
cout << "*****Welcome To My Calculator*****" << endl;
cout << "_______________________________________________" << endl;
cout << endl;

restart:
cout << "Please input the first number" << endl;
cin >> number1;

retry:
cout << "Please input an operation (+,-,*,/)" << endl;
cin >> operation;

cout << "Please input the second number" << endl;
cin >> number2;

switch (operation)
{
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
result = number1 / number2;
break;
default:
goto retry;
break;
}

cout << endl;

if (number2 == 0)
cout << "Error!!! " << endl;
else
cout << "The result is: " << result << endl;
cout << endl;

system("Pause");
system ("cls");
goto restart;
}
You don't make yourself clear. Do you want the user to enter "2+2=" and have your calculator parse that ?

There are many ways to put things in classes. I guess you could have an Equation class written so that your main is
1
2
3
Equation e;
e.read();
cout << "ther result is " << e.calc();
I would like them to be able to enter as many numbers as possible, for example, "2+2/5+6+9*5=".
Here's an older thread where I tried to explain how to do that: http://www.cplusplus.com/forum/beginner/36637/#msg199334
See my other posts in that thread too.
This doesn't tell you how to use classes for it, but when you have the algorithm figured out, slight restructuring should not be any trouble.
This is what I have so far. I'm not sure how to incorporate the class NCalculator into my program after declaring it.

These are some pointers I got from the professor, but I am still unsure how to do this:
"you're going to have a loop for your arithmetic cycle, and if
statements to determine which method inside your class you're going to
be calling."
___________________________________________________________________________
#include <iostream>
using namespace std;

class NCalculator
{
float a,b;
public:
void set_values(float n1, float n2) { a=n1; b=n2;}
float addition () {return (a+b);}
float subtraction () {return (a-b);}
float multiplication () {return (a*b);}
float division () {return (a/b);}
};


int main ()
{
float x, y, z;
char c;
NCalculator myCalc;

cout << "Please input the first number";
cin >> x;

cout << "Please input the second number";
cin >> y;

myCalc.set_values(x, y);
cout << x << '+' << y << '=' << myCalc.addition() << endl;
cout << x << '-' << y << '=' << myCalc.subtraction() << endl;
cout << x << '*' << y << '=' << myCalc.multiplication() << endl;
cout << x << '/' << y << '=' << myCalc.division() << endl;

system ("PAUSE");
return 0;
}
Last edited on
Like I said, make your main
1
2
3
Equation e;
e.read();
cout << "ther result is " << e.calc();
, then think about how to implement read() and calc() methods to make this work.
Topic archived. No new replies allowed.