New to C++, having trouble with loops

Hi everyone, I am very new to programming, and I am feeling stumped over a lab that we did in class last week. Here is a description of the problem and the pseudocode:

Problem:
Your State Dive Association presently scores its diving competitions with pencil and paper. They would like for you to design and develop a Dive Program in C++.
The paper forms that they presently use have the following:

Diver’s Name, City
JudgeScore1 - The scores entered are from 0 to 10.
JudgeScore2
JudgeScore3
JudgeScore4
JudgeScore5
DegreeOfDifficulty - This is assigned once for each diver.
OverAllScore - The overall score is the individual diver’s scores totaled and then divided by the degree of difficulty. The highest and lowest scores are removed as they are often skewed entries. Total the three scores left, divide them by 3, and then multiply that by the DegreeOfDifficulty. The degree of difficulty ranges from 1.00 to 1.67.

Display the diver's information and overall score.
When the competition is complete, there is a summary report created that lists the total number of divers and the average of the overall scores.

Pseudocode:

Write report heading

Loop as long as there are divers to process
Input diver's name and city
Initialize highest score, lowest score and total score

Using a do-while loop input the 5 judge's scores
Validate the score to ensure it is between 0 and 10
Add score to total
Determine highest and lowest scores

Input and validate the degree of difficulty
Calculate the overall diver's score

Display the diver's information and overall score
Add diver's overall score to the final score
Add 1 to the number of divers

Prompt the user if she wants to process another diver
End-Loop

Calculate the average score for all divers
Display the number of divers and the average score for all divers

I've attached the code that I came up with, though I feel like I might be missing the mark. I also keep getting several syntax errors when I try to build the solution:

1>------ Build started: Project: Lab 3, Configuration: Debug Win32 ------
1> Lab 3.cpp
lab 3.cpp(9): error C2059: syntax error : 'constant'
lab 3.cpp(47): error C2143: syntax error : missing ',' before ')'
lab 3.cpp(51): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(56): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(60): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(62): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(62): error C2143: syntax error : missing ',' before ')'
lab 3.cpp(70): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(75): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(79): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(81): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(81): error C2143: syntax error : missing ',' before ')'
lab 3.cpp(89): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(98): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(100): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(100): error C2143: syntax error : missing ',' before ')'
lab 3.cpp(108): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(113): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(117): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(119): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(119): error C2143: syntax error : missing ',' before ')'
lab 3.cpp(133): error C2143: syntax error : missing ';' before '{'
lab 3.cpp(137): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(149): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(159): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(160): error C2143: syntax error : missing ';' before '}'
lab 3.cpp(160): fatal error C1004: unexpected end-of-file found

If anybody has the time, can you please take a look at this and point me in the right direction? Any help is appreciated, I really don't want to fall behind 3 weeks into an 8 week course. Thanks!


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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	// Declare variables
	char continueLoop = 'Y', 'y';
	string diverName, diverCity;
	double highestScore = 0;
	double lowestScore =  0;
	double totalScore =0;
	double overallScore = 0;
	double diveDifficulty = 0;
	double diverTotal = 0;
	double avgScore = 0;
	double score1, score2, score3, score4, score5 = 0;
	int numberDivers = 0;
	
	// Report heading
	cout << "    Report to the Media" << endl;
	cout << " Event: Diving Competition" << endl;

	// Attempt to design loops
	while (continueLoop == 'Y' || continueLoop == 'y')
	{
		cout << "Enter the diver's name: ";
		getline(cin, diverName);
		cout << "Enter the diver's city: ";
		getline(cin, diverCity);

		// Input judge's scores
		// Judge 1
		do
		{
			int count = 0;
			cout << "Enter the score from Judge #1: ";
			cin >> score1;
			while (score1 < 0 || score1 > 10)
			{
				cout << "Invalid score. Enter a new score (Score Range: 1 - 10)" << endl;
				cout << "Enter the score from Judge #1: ";
				cin >> score1;
			}
			count++;
		} while (count < 1);
		
		// Judge 2
		do
		{
			int count = 0;
			cout << "Enter the score from Judge #2: ";
			cin >> score2;
			while (score2 < 0 || score2 > 10)
			{
				cout << "Invalid score. Enter a new score (Score Range: 1 - 10)" << endl;
				cout << "Enter the score from Judge #2: ";
				cin >> score2;
			}
			count++;
		} while (count < 1);
		if (score2 < score1)
			score2 = lowestScore;
		if (score2 > score1)
			score2 = highestScore;

		// Judge 3
		do
		{
			int count = 0;
			cout << "Enter the score from Judge #3: ";
			cin >> score3;
			while (score3 < 0 || score3 > 10)
			{
				cout << "Invalid score. Enter a new score (Score Range: 1 - 10)" << endl;
				cout << "Enter the score from Judge #3: ";
				cin >> score3;
			}
			count++;
		} while (count < 1);
		if (score3 < lowestScore)
			score3 = lowestScore;
		if (score3 > highestScore)
			score3 = highestScore;

		//Judge 4
		do
		{
			int count = 0;
			cout << "Enter the score from Judge #4: ";
			cin >> score4;
			while (score4 < 0 || score4 > 10)
			{
				cout << "Invalid score. Enter a new score (Score Range: 1 - 10)" << endl;
				cout << "Enter the score from Judge #4: ";
				cin >> score4;
			}
			count++;
		} while (count < 1);
		if (score4 < lowestScore)
			score4 = lowestScore;
		if (score4 > highestScore)
			score4 = highestScore;

		// Judge 5
		do
		{
			int count = 0;
			cout << "Enter the score from Judge #5: ";
			cin >> score5;
			while (score5 < 0 || score5 > 10)
			{
				cout << "Invalid score. Enter a new score (Score Range: 1 - 10)" << endl;
				cout << "Enter the score from Judge #5: ";
				cin >> score5;
			}
			count++;
		} while (count < 1);
		
		if (score5 < lowestScore)
			score5 = lowestScore;
		
		if (score5 > highestScore)
			score5 = highestScore;

		diverTotal = (score1 + score2 + score3 + score4 + score5) - highestScore - lowestScore;

		// Calculate degree of difficulty and overall diver's score
		cout << "Enter the degree of difficulty (1 - 1.67): ";
		cin >> diveDifficulty;
		while (1 > diveDifficulty && diveDifficulty > 1.67)
		{	
			cout << "Invalid degree of difficulty - Please reenter (Valid Range: 1 - 1.67)\n";
			cout << "Please enter the degree of difficulty (1 - 1.67): ";
			cin >> diveDifficulty;
		}
		overallScore = (diverTotal / 3) * diveDifficulty;

		// Display diver's information and overall score
		numberDivers++;
		overallScore += totalScore;
		cout << "Diver: " << diverName << endl;
		cout << "City: " << diverCity << endl;
		cout << "Overall Score: " << totalScore;

		cout << "Would you like to enter another diver (Y/N)?";
		cin >> continueLoop;
	}

	avgScore = totalScore / numberDivers;

	// Output event summary
	cout << "       Event Summary" << endl;
	cout << "Number of divers participating: " << numberDivers << endl;
	cout << "Average score of all divers: " << avgScore << endl;
	
	return 0;
}
Last edited on
closed account (48T7M4Gy)
char continueLoop = 'Y', 'y';

Looking at the errors line by line, look at the first one. What this line means is you have declared a variable called continue Loop and set it to the value to the value 'Y'. Then you have said , 'y' followed by the end of line ;

What does ,'y' mean? What are you trying to say?
Let's start with the error on line 9. You are attempting to assign two character values to a single character variable. A character variable can only hold one value at a time.

The next error is on line 47. You are checking the value of the variable count in your loop test condition. However, you declared that variable inside the loop. Since you declared the variable inside the loop, that variable is not accessible from outside the loop (or from the loop condition).
Hi,

I think you have put in a good effort, for someone at your level so you should be pleased about that :+). Here is some advice , which I hope you will see as being constructive, and can learn from. I worry a bit these days that my tone sometimes might come across as being a little direct or terse, but I always mean well, so here we go ....... 8+)

Apart from errors, you have a lot of repeated code - this means that there is a better way. Can you figure out how to write the code without repeating lines 35 to 47 five times, and line 63 to 66 four times? Hint check the condition of your do loop

A main pillar of coding is to not write repeated code - what if you had 12 judges, would you write that code 12 times? The solution for this particular problem is to do the same as what you did for the competitors - use a loop. There is a data structure called an array - but I am not sure if you have studied these yet - if you have, then now is a really good time to make use of them.

Also, read the pseudo code more carefully - is there a way of writing line 127 better?

Some other really good tips:

Always declare & initialise one variable per line. Uninitialised variables are a huge source of errors, and it is more maintainable & clearer to do one per line. On line 18, which of those variables are initialised to zero? Or another way, which is the only one that is initialised?

Always use braces in loops and if statements, even if there is only one line. This is a personal preference, but I still think it is a really good idea - it will save you one day when you add more code.

Don't have using namespace std; Google why that is so, and put std:: before each std thing.

Hope all goes well :+)
@kemort,

Okay I can see how that could cause a problem. I wasn't aware that you could only assign one character to a variable. I was trying to get it to accept either an uppercase or lowercase 'Y', when the program asks if you would like to add another diver.

@squished18,

I think that this is getting at the core of the problem I am having with this problem. The directions tell me that I need to create a DO-WHILE loop to accept the judge's scores, and to evaluate if the score is valid (1-10). I can understand that the DO portion should be asking for the scores and evaluating them, but I can't fathom what the WHILE condition should be.

Thanks for the replies guys!
closed account (48T7M4Gy)
@stevanc

I thought so, but a variable can only take one value at a time especially when it is being initialised.

The trick with error checking is to address the first one, recompile it and quite often most disappear.

To get your program to run there are only two errors to fix - hint: read squished's comment. :-)

On the broader scale, once you get it to run address the sound advice from IdeasMan. Your obvious hard work will be rewarded.

Good luck :-)

PS You've already solved one of the errors ...
Last edited on
... but I can't fathom what the WHILE condition should be.


TheIdeasMan wrote:
Can you figure out how to write the code without repeating lines 35 to 47 five times, and line 63 to 66 four times? Hint check the condition of your do loop


Does this help?

Also, try to avoid magic numbers like 5 in your code - make them a const variable instead:

const unsigned int NumJudges = 5;

That last part was also a massive clue !

Edit:
Also realise about my earlier advice: I am trying to get you to do less work, not more :+)
Last edited on
@kemort & TheIdeasMan,

Thanks again for your help.. I've been tinkering and have come up with something that is a little closer to functioning. My only question is inputting and storing the scores, so that I can access them individually to eliminate the highest and lowest from the range. Is this where an array may come into play?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
		// Input judge's scores
		const unsigned int numJudges = 0;
		do
		{
			cout << "Enter the score from Judge #" << numJudges+1 << ": ";
			cin >> score1;
			while (score1 < 0 || score1 > 10)
			{
				cout << "Invalid score. Enter a new score (Score Range: 1 - 10)" << endl;
				cout << "Enter the score from Judge #" << numJudges+1 << ": ";
				cin >> score1;
			}
			numJudges++;
		} while (numJudges < 5);
Last edited on
closed account (48T7M4Gy)
Make sure you keep separate copies of your working code - something you can fall back on.

You're on the right track to eliminating a lot of your unnecesary duplicated code. Give it a go. Where you have multiple things of the same type , an arrays are a good way to go.
Hi,

It is possible to keep track of the current maximum & minimum without using an array (if you are not allowed to use an array). Set the maximum & minimum to equal the first number, then test to see if each consecutive number is larger or smaller respectively - if it is, then assign this value to be the new min or max.

The process is almost the same with an array, but the advantage is you have stored all the values.

Regards
Topic archived. No new replies allowed.