- Joined
- Apr 14, 2012
- Messages
- 2,901
So I have recently started learning C++, and studied some of the basics. Using the basic knowledge I have for functions and parameters, I created this basic one-way calculator that takes two ints, x and y, and based on the user input (add, sub, mult), operates on those two numbers and gives the answer.
Though this only calculates once. I don't know how to make it keep accepting numbers indefinitely, but I would like to find out how.
Though this only calculates once. I don't know how to make it keep accepting numbers indefinitely, but I would like to find out how.

Code:
#include <iostream>
using namespace std;
int calculator( int x, int y, string s);
int main()
{
int x;
int y;
string op;
cout << "Enter an operation: (maybe mult, add, sub)" << endl;
getline( cin, op);
if( op == "add"){
cout << "You have chosen Addition as your function" << endl;
cout << "Please enter the two numbers:" << endl;
cin >> x; cin >> y;
calculator( x, y, op);
} else if( op == "sub"){
cout << "You have chosen Subtraction as your function" << endl;
cout << "Please enter the two numbers:" << endl;
cin >> x; cin >> y;
calculator( x, y, op);
} else if ( op == "mult"){
cout << "You have chosen Multiplication as your function" << endl;
cout << "Please enter the two numbers:" << endl;
cin >> x; cin >> y;
calculator( x, y, op);
} else {
cout << "You have not entered a valid operation" << endl;
}
return 0;
}
int calculator( int x, int y, string s)
{
int answer;
if( s == "add"){
answer = x + y;
cout << answer << endl;
} else if( s == "mult"){
answer = x * y;
cout << answer << endl;
} else if (s == "sub") {
answer = x - y;
cout << answer << endl;
}
return answer;
}