How do I add a while loop ( ) on calculator

Hi, I am taking C++ as a class and so I am very new at this. For our new assignment I have to add a while loop ( ). I tried doing this, but it kept saying I had an error in my build.

This is the code I had to add on from a previous assignment we had done.
This is the before.

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
// Calculator

// Include the iostream library
#include <iostream>

// Use the standard namespace
using namespace std;

void main ( )
{
	// Declare the variables
	float Number_1;
	float Number_2;
	float Result;
	int Which_Calculation;
	// Give instructions
	cout << "Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide. " << endl;
	cin >> Which_Calculation;

	// Get numbers
	cout << "Please enter the first number." << endl;
	cin >> Number_1;
	cout << "Please enter the second number." << endl;
	cin >> Number_2;


	if (Which_Calculation == 1)
	{
		// Calculate the result
		Result = Number_1 + Number_2;
	}

	if (Which_Calculation == 2)
	{
		// Calculate the result
		Result = Number_1 - Number_2;
	}

	if (Which_Calculation == 3)
	{
		// Calculate the result
		Result = Number_1 * Number_2;
	}

	if (Which_Calculation == 4)
	{
		// Calculate the result
		Result = Number_1 / Number_2;
	}

	// Print the answer is...
	cout << "The answer is..." << endl;

	// Print the result
	cout << Result << endl;
	system ("PAUSE");

}


This is when I tried inputting the while loop ( )

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
// Calculator

// Include the iostream library
#include <iostream>

// Use the standard namespace
using namespace std;

void main ( )
{
	// Declare the variables
	float Number_1;
	float Number_2;
	float Result;

	int Which_Calculation;

	while (Variable_Name == Result)
	{
	//This code repeats until the condition is no longer true
	


	// Give instructions
	cout << "Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide. " << endl;
	cin >> Which_Calculation;

	// Get numbers
	cout << "Please enter the first number." << endl;
	cin >> Number_1;
	cout << "Please enter the second number." << endl;
	cin >> Number_2;


	if (Which_Calculation == 1)
	{
		// Calculate the result
		Result = Number_1 + Number_2;
	}

	if (Which_Calculation == 2)
	{
		// Calculate the result
		Result = Number_1 - Number_2;
	}

	if (Which_Calculation == 3)
	{
		// Calculate the result
		Result = Number_1 * Number_2;
	}

	if (Which_Calculation == 4)
	{
		// Calculate the result
		Result = Number_1 / Number_2;
	}

	// Print the answer is...
	cout << "The answer is..." << endl;

	// Print the result
	cout << Result << endl;
	system ("PAUSE");

	}
}


Can anyone point out my mistakes please?
Last edited on
Well, look at line 18 looks like you copied it from an example. It didn't mean to literally put those for the param. It meant to fill in an actual variable name then a condition and to execute while the condition is true.

You could do something like

1
2
3
4
5
6
7
8
9

char again = 'y';

while( again == 'y' )
{
    //do stuff
    std::cout << "again(y/n)? ";
    std::cin >> again;
}
closed account (Dy7SLyTq)
your missing a } at line 62. your error was probably something like expected unqualified id
Another thing to mention you should be using int main and not void main. void main is not the standard.
Thank you! I did what you recommended and it works now. Thanks a lot; it helped me understand it more.
Topic archived. No new replies allowed.