here is a revised version. study it. and learn from it
#include <iostream>
#include <conio.h>
using namespace std;
void add(int,int); // This is a function prototype. it lets the compiler
// know your gonna use this function later.
void subtract(int, int); // You should tried to make the code more modular.
void multiply(int, int); // Try to put some of the operations in 'main' into
void divide(int,int); // it own function.
int main()
{
char repeat; // use this for the 'while' loop so the user chooses when exit the loop
double answer;
int option;
double num1;
double num2;
do{
cout << "Enter operator <1=add 2=subtract 3=multiply 4=divide>" << endl;
cin >> option;
while(option <= 0 || option > 4) // use a 'while' loop to make sure user doesn't type crap
{
cout<< "Enter the real operator you dingbat. " << endl;
cin >> option;
}
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
switch(option) // you should also learn to use the 'switch' statement rather than a bunch of 'if/else'
{
case 1:
add(num1,num2); // This is a function call. unless you call the function in 'main' it won't run
break;
case 2:
subtract(num1,num2); // Variables defined in 'main' can't be used in other function so
break; // pass them in as arguments.
case 3:
multiply(num1,num2);
break;
case 4:
divide(num1,num2);
break;
default:
break;
}
cout << "Would you like another go?[Y/n] : ";
cin >> repeat;
while (repeat != 'y' && repeat != 'Y' && repeat != 'n' && repeat != 'N') //to make sure the user types either 'y' or 'n'
{
cout << "Please choose [Y/n]: ";
cin >> repeat;
}
} while(repeat == 'y' || repeat == 'Y');
getch(); // the same as 'cin.get()' is in 'conio.h'
return 0;
}
void add(int num1,int num2) // Here is where you define the function
{
cout << "answer is" << endl;
cout << num1 + num2 << endl;
cout << endl;
}
void subtract(int num1, int num2) // When a value is passed from 'main' you must redeclare it in the parenthesis
{
cout << "answer is" << endl;
cout << num1 - num2 << endl;
cout << endl;
}
void multiply(int num1, int num2) // You can choose what the function should return to 'main'
{ // 'void' means it doesn't return any value. 'int' means it returns an integer
cout << "answer is" << endl;
cout << num1 * num2;
cout << endl;
}
void divide(int num1,int num2)
{
cout << "answer is" << endl;
cout << num1 / num2 << endl;
cout << endl;
}