Originally Posted by
MOH123
hmm i dont get that, nothing happens when i use that code. could you explain better? like where to i put it and stuff?
-thanks
Take his code, and add your code (to get 2 values and add them, and the add function) to where he had the commented line (your code is the red part):
Code:
#include <iostream>
using namespace std;
int add (int a, int b);
int sub (int c, int d);
int mult (int e, int f);
int divi (int g, int h);
int main() {
int one, two;
char op;
int repeat;
int a;
int b;
int c;
int d;
int select;
while(1) {
cout << "Enter the operator: ";
cin >> op;
if(op == 'q') break;
cout<<"enter two numbers: ";
cin>> a >> b;
cout<<"="<<add (a,b) <<"\n";
cout<<"\n";
cin.get();
}
return 0;
}
int add (int a,int b)
{
return a + b;
}
int sub (int c, int d)
{
return c - d;
}
int mult (int e, int f)
{
return e * f;
}
int divi (int g, int h)
{
return g / h;
}
Although doing this now only lets you use the add function, and not the subtract/multiple/divide functions. So I would add a switch statement, and if the cin variable was == d, it would go to case 'd' and divide the 2 numbers, same thing goes for m, s, and a.
Code:
#include <iostream>
using namespace std;
int add (int a, int b);
int sub (int c, int d);
int mult (int e, int f);
int divi (int g, int h);
int main() {
char op;
int a;
int b;
while (1) {
cout << "Enter the operator: ";
cin >> op;
if(op == 'q')
break;
cout<<"enter two numbers: ";
cin>> a >> b;
switch(op) {
case 'a':
cout<<"="<<add (a,b) <<"\n" << endl;
continue;
case 's':
cout<<"="<<sub (a,b) <<"\n" << endl;
continue;
case 'm':
cout<<"="<<mult (a,b) <<"\n" << endl;
continue;
case 'd':
cout<<"="<<divi (a,b) <<"\n" << endl;
continue;
default:
cout << "Nothing was entered\n" << endl;
continue;
} //end the switch statement
break;
} //end the while statement
cin.get();
return 0;
}
int add (int a,int b)
{
return a + b;
}
int sub (int c, int d)
{
return c - d;
}
int mult (int e, int f)
{
return e * f;
}
int divi (int g, int h)
{
return g / h;
}
So this basicly says it will keep asking the user the operator and the numbers to multiply, until when asked for the operator the user enters: q. Entering a will do the add function, m will do multiply, d will do division, and s will do subtraction. Entering nothing will go to default. The continue keyword is used to go back up to the top of the loop (top of the while statement). So after finishing the addition/etc it will go back up to the top of the while(1) and ask for the operator again.