Need advice on nested while loops.

Hello everyone,

This is my first time posting on the forums. I am a beginner in the C++ language and been having some difficulty troubleshooting my assignment for class. I have been working with while loops and nested while loops, but can not seem to grasp the process. Here is what I am having issues with.

// get user info
cout << " 100 Meter Race Results " << endl;
cout << " Are you ready to enter data for the first heat(Y/N): " << endl;
cin >> answer;

// set perimeters.
while (answer != 'N')
{
while (points != -1)
{
runner++;
heat = 1;
cout << " Enter time for runner " << runner << " in Heat " << heat << " (-1 to end heat) " << endl;
cin >> points;
}
if (points > mostpoints)
mostpoints = points;
if (points < leastpoints)
leastpoints = leastpoints;

cout << " Best time in Heat " << heat << " was runner # " << mostpoints << endl;
cout << " Are you ready to enter data for the first heat(Y/N): " << endl;
cin >> answer;
}

system("pause");
return 0;
}

I can not seem to get the Y/N loop to execute. I apologize for the layout of the post. I am trying to figure out how to format in code. If someone could please give me a hint of why my outer while loop is not working it would be greatly appreciated.
Last edited on
Can you please use tags: http://cplusplus.com/articles/firedraco1/.

Could you also post the entire code and what the program is supposed to do please.
Sorry about that. The program is supposed ask the user for time of the runners in each heat. (100 meter race) Then display the runner numbers and their race times using a sentinel-controlled while loop. Then add the code to handle multiple heats.

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
#include <iostream>
using namespace std;

int main()
{
     char answer;
     int total = 0, points = 0;
     int runner = 0;
     int heat = 0;
     double mostpoints = 1;
     double leastpoints = 1;
	
// get user info
	cout << " 100 Meter Race Results " << endl;
	cout << " Are you ready to enter data for the first heat(Y/N): " << endl;
	cin >> answer;

// set perimeters.
	while (answer != 'N')
	{	
	     while (points != -1)
	     {
		  runner++;
		  heat = 1; 
		  cout << " Enter time for runner " << runner << " in Heat " << heat << " (-1 to end heat) " << endl;
		  cin >> points;
	      }
		if (points > mostpoints)
		mostpoints = points;
		if (points < leastpoints)
		leastpoints = leastpoints;
			 
		cout << " Best time in Heat " << heat << " was runner # " << mostpoints << endl;
		cout << " Are you ready to enter data for the first heat(Y/N): " << endl;
		cin >> answer;
	}

system("pause");
return 0;
}


I understand that this is still a work in progress by far, but at this moment I am just trying to figure out why my nested while loop is working but not the outer loop.
Hello!

Just exactly what is it you are having problems with? I compiled it (well, I had to comment out system("pause")) and ran it. It seems to work fine for me, or am I missing something?

Best,

HMW
Last edited on
One problem is line 19. You have the loop to run while the answer != 'N' .. but how about if the user enters 'n' than it'll still run.

Also check your if statements, you may have entered the wrong variables.

Also your program won't loop at the bottom if you enter 'Y' again.
Last edited on
Line 31: leastpoints = leastpoints;
Topic archived. No new replies allowed.