Pause at mathematical operations

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?

That's the whole code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <cstdlib>
#include <iostream>

using namespace 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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <cstdlib>
#include <iostream>

using namespace 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;
}


Topic archived. No new replies allowed.