Hey guys, I got a question, I just programmed a kind of Calculator, but when the result is going to be given, the console window just closes, and I already tried to put system("PAUSE"); in some places, but it did not work.
Where should I put it in order to pause the window?
#include <cstdlib>
#include <iostream>
usingnamespace std;
float addition(float num1, float num2)
{
return num1 + num2;
}
float subtraction(float num1, float num2)
{
return num1 - num2;
}
float multiplication(float num1, float num2)
{
return num1 * num2;
}
float division(float num1, float num2)
{
return num1 / num2;
}
int main()
{
float number1;
float number2;
int choice;
cout << "Choose your opperation. 1 = Adding. 2 = Subtracting. 3 = Multiplying. 4 = Dividing" << endl;
cout << "Choice: ";
cin >> choice;
if(choice==1)
{
//Addition
cout << "You are adding. Enter the first number you would like to add. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << addition(number1, number2) << endl;
}
else
{
if(choice==2)
{
//Subtraction
cout << "You are subtracting. Enter the first number you would like to subtract. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << subtraction(number1, number2) << endl;
}
else
{
if(choice==3)
{
//Multiplication
cout << "You are adding. Enter the first number you would like to multiply. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << multiplication(number1, number2) << endl;
}
else
{
//Division
cout << "You are dividing. Enter the first number you would like to divide. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << division(number1, number2) << endl;
}
}
}
}
Line 83 is the end of function main.
There you could add your pause / cin / getch, just before the closing brace.
If you expand this program, you might use a while loop to get more input from the user, then the program will have to pause for the user to enter the next data, so it's less of an issue.
You might also use a switch-case instead of a series of if-else statements.
Here I used an enum, which is not essential, it just makes the case statements look better. Also I used type double instead of float. (there's rarely any good reason to use float).
#include <cstdlib>
#include <iostream>
usingnamespace std;
double addition(double num1, double num2)
{
return num1 + num2;
}
double subtraction(double num1, double num2)
{
return num1 - num2;
}
double multiplication(double num1, double num2)
{
return num1 * num2;
}
double division(double num1, double num2)
{
return num1 / num2;
}
int main()
{
double number1;
double number2;
enum operation {add=1, subtract, multiply, divide};
int choice;
cout << "Choose your operation. 1 = Adding. 2 = Subtracting. 3 = Multiplying. 4 = Dividing" << endl;
cout << "Choice: ";
cin >> choice;
switch ( (operation) choice)
{
case add:
cout << "You are adding. Enter the first number you would like to add. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << addition(number1, number2) << endl;
break;
case subtract:
cout << "You are subtracting. Enter the first number you would like to subtract. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << subtraction(number1, number2) << endl;
break;
case multiply:
cout << "You are muliplying. Enter the first number you would like to multiply. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << multiplication(number1, number2) << endl;
break;
case divide:
cout << "You are dividing. Enter the first number you would like to divide. \n Number 1: ";
cin >> number1;
cout << " Enter the second number. \n Number 2: ";
cin >> number2;
cout << " The answer is: " << division(number1, number2) << endl;
break;
default:
cout << "That isn't a valid choice" << endl;
}
cin.ignore(100,'\n');
cin.get();
return 0;
}