System skips user input line restarting loop

I have a small problem in my program. The program runs fine, but when the user is prompted to enter a second diver it skips the diver's name entry and goes right to the diver's city name entry. This is only my third week of C++ and what we have to deal with is little. We've learned if/else statements, nested if's, the 3 loops, some data manipulation- basically all you see here. One thing I did add instead of a 'continue' in my if's I used a few 'goto' lines, though we've not learned them yet. Not sure if my teacher will knock off points for it. Can anyone help or point me in the right direction on the user entry for the diver name problem?


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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Date: March 17, 2011
// Diver Scoring Program
// Determines individual scores for divers and shows average of all divers
#include <iostream>
#include <string>

using namespace std;

int main()
{
	// define variables
	double judge = 1; // judge counter
	double score, average,overallAverage;
	double total = 0;
	double totalAverage = 0;
	char mainLoop = 'y';
	int loop = 1;
	double difficulty = 0;
	double max = 0;
	double min = 0;
	string diverName;
	string diverCity;
	int totalDivers = 0;

	// display to diver
	cout << "Diving Competition\n" << endl;

	// main loop setup
	while (mainLoop == 'y')
	{
		// loop resets
		diverName.clear();
		diverCity.clear();
		judge = 1;
		loop = 1;

		// diver entry
		cout << "\nEnter diver name: " << diverName;
		getline (cin, diverName);
		cout << "Enter diver city: " << diverCity;
		getline (cin, diverCity);
		ScoreF:
		cout << "\nEnter score from judge #" << judge << ": ";
		cin >> score;
		if (score <= 0 || score > 10)
		{
			cout << "Please reenter (Valid Range: 0 - 10)" << endl;
			goto ScoreF;
		}

		// setsup find min/max for diver's 5 scores and adds them together
		min = score;
		max = score;
		total = score;
  
		// second loop setup for score input
		do
		{
			judge++;
			ScoreS: // label if input error
			cout << "Enter score from judge #" << judge << ": ";
			cin >> score;
			if (score <= 0 || score > 10)
			{
				cout << "Please reenter (Valid Range: 0 - 10)" << endl;
				goto ScoreS;
			}
			
			if(score > max)
				max = score;
			if(score < min)
				min = score;
			total = score + total;
			loop++;
		}
		while (loop < 5);

	// diver difficulty with error message if out of range
	Difficulty: // label if input error
	cout << "Enter your difficulty: " << endl;
	cin >> difficulty;
	if (difficulty < 1 || difficulty > 1.67)
	{
		cout << "Please reenter (Valid Range: 1 - 1.67)" << endl;
		goto Difficulty;
	}
	    
	// Display diver and city with overall score
	cout << "\nDiver: " << diverName << " City: " << diverCity << endl;
	average = (total - (min + max)) / 3 * difficulty;
	cout << "Overall score was " << average << endl;
	
	// increase diver count and display highest diver average
	totalDivers ++;
	cout << "Number of divers participating: " << totalDivers << endl;
	totalAverage = average + totalAverage;
	overallAverage = (totalAverage / totalDivers);
	cout << "Average score of all divers: " << overallAverage << endl;

	// entry for next diver or exit main loop
	cout << "\nEnter another diver?" << endl;
	cout << "Press y to continue and n to exit" << endl;
	cin >> mainLoop;
			
	}

return 0;

}
Last edited on
Thanks for the link, but I went for a short fix so as not to rewrite a lot of the code. Next project I'll keep the information you passed on in mind. My fix was this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// declared another string
string pause = "";

// outside of main loop added a message to the user
cout << "Press 'ENTER' to begin";

	// main loop setup
	while (mainLoop == 'y')

// in the loop where data entry was to begin for user I added the command 
// for the 'ENTER' message
        getline (cin, pause);
	cout << "\nEnter diver name: " << diverName;


Now the second time through it skips the ENTER for user and jumps instead to diver name entry.
Topic archived. No new replies allowed.