using namespace std;
#include <iostream>
void main ()
{
//Declares 3 Variables to be held in the program.
float Variable_1;
float Variable_2;
float Result;
int Operation = 1;
int Rerun = 1;
while (Rerun == 1);
{
//Asks the user what operation they wish to do.
cout<<"Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide."<<endl;
cin>>Operation;
//The user, using cin, Inputs 2 variables
cout<<"Input number 1:"<<endl;
cin>> Variable_1;
cout<<"Input Number 2:"<<endl;
cin>>Variable_2;
cout<<"The answer is: "<<endl;
//Now use Var_1 and Var_2 to calculate the result. It's easier in VB with the elseif command, but I couldn't find it in C++.
//Addition
if (Operation == 1)
{
Result = Variable_1 + Variable_2;
cout << Result << endl;
}
//Subtraction
if (Operation == 2)
{
Result = Variable_1 - Variable_2;
cout << Result << endl;
}
//Multiplication
if (Operation == 3)
{
Result = Variable_1 * Variable_2;
cout << Result << endl;
}
//Division
if (Operation == 4)
{
Result = Variable_1 / Variable_2;
cout << Result << endl;
}
cout<<"Do you want to do another equasion? (1 for yes, 2 for no)"<<endl;
cin>>Rerun;
}
}
Refactorings
No refactoring yet !
Josiah Martin
November 16, 2010, November 16, 2010 04:35, permalink
In line 13 you have a semicolon after the while declaration, causing the loop to terminate. Removing the semicolon should make it run correctly.
Jonathan
December 13, 2010, December 13, 2010 13:49, permalink
using namespace std;
#include <iostream>
void main ()
{
//Declares 3 Variables to be held in the program.
float Variable_1;
float Variable_2;
float Result;
int Operation = 1;
int Rerun = 1;
while (Rerun == 1)
{
//Asks the user what operation they wish to do.
cout<<"Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide."<<endl;
cin>>Operation;
//The user, using cin, Inputs 2 variables
cout<<"Input number 1:"<<endl;
cin>> Variable_1;
cout<<"Input Number 2:"<<endl;
cin>>Variable_2;
cout<<"The answer is: "<<endl;
//Now use Var_1 and Var_2 to calculate the result. It's easier in VB with the elseif command, but I couldn't find it in C++.
//Addition
if (Operation == 1)
{
Result = Variable_1 + Variable_2;
cout << Result << endl;
}
//Subtraction
if (Operation == 2)
{
Result = Variable_1 - Variable_2;
cout << Result << endl;
}
//Multiplication
if (Operation == 3)
{
Result = Variable_1 * Variable_2;
cout << Result << endl;
}
//Division
if (Operation == 4)
{
Result = Variable_1 / Variable_2;
cout << Result << endl;
}
cout<<"Do you want to do another equasion? (1 for yes, 2 for no)"<<endl;
cin>>Rerun;
}
}
Ants
December 14, 2010, December 14, 2010 19:47, permalink
Sometimes refactoring can go overboard, but I had a good time doing this to make it more object oriented, and trying to separate I/O concerns from computation concerns. :-)
There is still one more level of refactoring that can be done to combine the common pattern between GetOperation() and RunAgain(), but that involves making MenuItem and Menu classes.
#include <iostream>
using namespace std;
class Operation
{
public:
virtual float DoOperation(float a, float b) = 0;
};
class Add : public Operation
{
public:
virtual float DoOperation(float a, float b)
{
return a + b;
}
};
class Subtract : public Operation
{
public:
virtual float DoOperation(float a, float b)
{
return a - b;
}
};
class Multiply : public Operation
{
public:
virtual float DoOperation(float a, float b)
{
return a * b;
}
};
class Divide : public Operation
{
public:
virtual float DoOperation(float a, float b)
{
return a / b;
}
};
class InvalidInputException
{
};
template<class T>
T PromptInput(const char * prompt)
{
T input;
cout << prompt << endl;
cin >> input;
if (cin.fail() || cin.bad())
throw InvalidInputException();
return input;
}
Operation * GetOperation()
{
while (true)
{
switch(PromptInput<int>("Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide."))
{
case 1:
return new Add();
case 2:
return new Subtract();
case 3:
return new Multiply();
case 4:
return new Divide();
}
}
}
bool RunAgain()
{
while (true)
{
switch(PromptInput<int>("Do you want to do another equation? (1 for yes, 2 for no)"))
{
case 1:
return true;
case 2:
return false;
}
}
}
void main()
{
try
{
do
{
Operation * operation = GetOperation();
float v1 = PromptInput<float>("Input number 1:");
float v2 = PromptInput<float>("Input number 2:");
cout << "The answer is: " << operation->DoOperation(v1, v2) << endl;
delete operation;
} while (RunAgain());
}
catch(InvalidInputException)
{
cerr << "Invalid Input. Aborting now." << endl;
}
}
Matthew G
May 31, 2011, May 31, 2011 21:40, permalink
Here is my generic oop version.
#include <iostream>
using namespace std;
template <class T>
class operations
{
public:
T perform(int perf, T lhs, T rhs)
{
T result;
if(perf == 1)
{
result = lhs + rhs;
}
else if(perf == 2)
{
result = lhs - rhs;
}
else if(perf == 3)
{
result = lhs * rhs;
}
else if(perf == 4)
{
result = lhs / rhs;
}
return result;
}
};
int main()
{
float Variable_1;
float Variable_2;
float Result;
int Operation = 1;
int Rerun = 1;
operations<float>* op = new operations<float>();
while(Rerun == 1)
{
cout << "Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
cin >> Operation;
cout << "Input number 1:" << endl;
cin >> Variable_1;
cout << "Input Number 2:" << endl;
cin >> Variable_2;
cout << "The answer is: ";
Result = op->perform(Operation, Variable_1, Variable_2);
cout << Result << endl;
cout << "Do you want to do another equation? (1 for yes, 0 for no)";
cin >> Rerun;
}
return 0;
}
Matthew G
May 31, 2011, May 31, 2011 21:41, permalink
Here is my generic oop version.
#include <iostream>
using namespace std;
template <class T>
class operations
{
public:
T perform(int perf, T lhs, T rhs)
{
T result;
if(perf == 1)
{
result = lhs + rhs;
}
else if(perf == 2)
{
result = lhs - rhs;
}
else if(perf == 3)
{
result = lhs * rhs;
}
else if(perf == 4)
{
result = lhs / rhs;
}
return result;
}
};
int main()
{
float Variable_1;
float Variable_2;
float Result;
int Operation = 1;
int Rerun = 1;
operations<float>* op = new operations<float>();
while(Rerun == 1)
{
cout << "Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
cin >> Operation;
cout << "Input number 1:" << endl;
cin >> Variable_1;
cout << "Input Number 2:" << endl;
cin >> Variable_2;
cout << "The answer is: ";
Result = op->perform(Operation, Variable_1, Variable_2);
cout << Result << endl;
cout << "Do you want to do another equation? (1 for yes, 0 for no)";
cin >> Rerun;
}
return 0;
}
Without the while loop, it runs just fine. With the loop, however, it just displays a blank box when I debug.