Program outputs answer a billion times and breaks

It's a calculator but it outputs the answer a bunch of times and breaks. I just need it to output once. Like if it's 5 + 5 then it outputs 10 over and over instead of outputting it once. how do I fix it? thnx

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
#include <iostream>

using namespace std;

double calculator(double num1, double num2, char operation);

int main() {
	double num1, num2, answer;
	char operation;

	cout << "Enter first number: " << endl;
		cin >> num1;

	cout << "Enter second number: " << endl;
		cin >> num2;

		cout << "Enter operation: (+, -, *, or /) " << endl; 
		cin >> operation;


		cout << num1 << " " << operation << " " << num2 << " = " << endl;
		cout << calculator(num1, num2, operation) << endl;

		system("pause");
}

double calculator(double num1, double num2, char operation)
{
	
	if (operation == '+')
	{
		cout << num1 + num2 << endl;
	}

	else if (operation == '-')
	{
		cout << num1 - num2 << endl;
	}

	else if (operation == '*')
	{
		cout << num1 * num2 << endl;
	}

	else if(operation == '/')
	{
		cout << num1 / num2 << endl;
	}
	return calculator(num1, num2, operation);

}
What do you think line 49 does?
Line 49: calculator is calling itself recursively with no way to exit.
oh I guess print the answer a bunch of times. I did return 0; before which worked but then it added a 0 to every answer like 5 + 5 = 100
Hi,

Why the recursive call on line 49?

Instead of doing std::cout inside the function, just return the answer.

Need some calls to the ignore function after std::cin, otherwise it reads the newline.

Why does everyone seem to forget about check for division by zero?

The parameters to your function should be const:

27
28
29
30
31
32
double calculator(const double num1, 
                   const double num2, 
                   const char operation)
{
	
	if (operation == '+')


In the function, provide an else so as to catch bad input.

Good Luck !!
Last edited on
Thank you all. Now it doesn't seem to catch the division by zero. It outputs this:
"Invalid.
10 / 9 = -nan(ind)"
I just want it to say "Invalid" not the equation


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
#include <iostream>

using namespace std;

double calculator(double num1, double num2, char operation);

int main() {
	double num1, num2, answer;
	char operation;
	char input;

	cout << "Enter first number: " << endl;
		cin >> num1;

	cout << "Enter second number: " << endl;
		cin >> num2;

		cout << "Enter operation: (+, -, *, or /) " << endl; 
		cin >> operation;


		cout << "\n" << num1 << " " << operation << " " << num2 << " = " << calculator(num1, num2, operation) << endl;

		cout << "Repeat? y for yes, any other key to exit: " << endl;
			cin >> input;
		

			while (input == 'y')
			{
				system("CLS");
				cout << "Enter first number: " << endl;
				cin >> num1;

				cout << "Enter second number: " << endl;
				cin >> num2;

				cout << "Enter operation: (+, -, *, or /) " << endl;
				cin >> operation;


				cout << "\n" << num1 << " " << operation << " " << num2 << " = " << calculator(num1, num2, operation) << endl;

				cout << "Repeat? y for yes, any other key to exit: " << endl;
				cin >> input;
			
			}


		system("pause");
}

double calculator(const double num1, const double num2, const char operation)
{
	
	if (operation == '+')
	{
		return num1 + num2;
	}

	else if (operation == '-')
	{
		return num1 - num2;
	}

	else if (operation == '*')
	{
		return num1 * num2;
	}

	else if ((operation == '/') && (num1 != 0))
	{
		return num1 / num2;
	}

	else
		cout << "Invalid. ";

	

}
Hi,

See this message for some clues:

http://www.cplusplus.com/forum/beginner/189816/#msg918996

With your while loop, lines 12 to 25 are the same as lines 30 to 44. can you figure out how to avoid this repetition?
Topic archived. No new replies allowed.