C++ homework issues using loops

My first question is what would be the best way to find the low score in this program, because I can only find the high score and the last score that was entered by the user. I am also not understanding why when after the first iteration of the first for loop it skips the first question where it asks "Enter the diver's name: " it prints that on the same line as "Enter the diver's city: " and never allows the user to enter the diver's name after the initial iteration of the loop, it will only allow you to enter the diver's city.

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

void main ()
{	
	cout<<"\n\tReport to the media"<<endl;
	cout<<"Event: Diving competition\n"<<endl;
	int i=0;
	int z=0;
	double lowScore=0;
	double highScore=0;
	double totalScore=0;
	double grossScore=0;
	double overall=0;
	double allOverall=0;
	int divers=0;
	int allDivers=0;
	string name;
	string city;

	for(i; i >= 0 ; i++)
	{//Asks the user for the name and city of each diver.
		cout<<"Enter the diver's name: ";
		getline(cin,name);

		cout<<"Enter the diver's city: ";
		getline(cin,city);

		//Asks the score from each judge five times.
		for (int judgeNumber=1; judgeNumber <6;judgeNumber++)
		{
			double judgeScore=0;
			cout<<"Enter the score given by the judge #"<< judgeNumber<<":";
			cin>> judgeScore;
			//find what the lowest score and highest score recorded from each judge.
			if (judgeScore < 10)
				lowScore=judgeScore;
			else if(lowScore>highScore)
				highScore=lowScore;
			if(judgeScore> highScore)
				highScore=judgeScore;
			//Pulls up an error message for an inccorect input. 
			if(judgeScore < 0 || judgeScore > 10)
			{				
				cin.fail();
				int x=0;
				x+=judgeNumber-1;
				judgeNumber=x;
				cout<<"Invalid score - Please reenter (Valid Range: 0-10)"<<endl;
				cin.clear();
			}
			//takes the total from each score from the judges and subtracts the low score and the high score. 
			totalScore+=judgeScore;
			grossScore=totalScore - highScore - lowScore;
		}
		cout<<highScore<<endl;
		cout<<lowScore<<endl;
		cout<<totalScore<<endl;
		cout<<grossScore<<endl;
		for (int y=0; y < 1 ; y++)
		{
			double difficulty;
			cout<<"What was the degree of difficulty? ";
			cin>>difficulty;
			if(difficulty < 0 || difficulty > 1.67)
			{	
				int j = 0;
				j+=y-1;
				y=j;
				cout<<"Invalid degree of difficulty - Please reenter (Valid Range: 1 - 1.67)"<<endl;
				continue;
			}
			//Divides the non-skewed score by the difficulty. 
			overall= (grossScore / difficulty);
		}
		allOverall+=overall;
		//Prints out each divers summarry.
		cout<<overall<<endl;
		cout<<"\nDiver: "<<name<<", City: "<<city<<endl;
		cout<<"Overall score was "<<fixed<<setprecision(2)<<overall<<endl;
		char anotherDiver;
		cout<<"\nDo you want to process another diver (Y/N)?";
		cin>>anotherDiver;
		
		//asks the user if you want to input another diver. 
		if(anotherDiver =='y'|| anotherDiver=='Y')
		{	
			i+=1;
		}
		else
		{
			break;
		}

	}
	//sums how many divers participated
	if(i=0)
		divers= 1;
	else
		divers=i;
	allDivers+=divers;
	//adds up each diver participating. 
	cout<<"\n\tEVENT SUMMARRY"<<endl;
	
	cout<<"\nNumber of divers participating: " <<allDivers<<endl;
	double average = (allOverall/allDivers);

	cout<<"Average score of all divers: "<<average<<endl;
}


Last edited on
Line 24 will loop until i is less than 0. This will happen after i overflows it maximum value - a little more than 2 billion on a 32 bit system. If you want to input divers until the user says otherwise then just make this loop while(true)...

You should initialize highscore, lowscore and totalScore at line 31. Right now the previous high/low scores will be set each time through the loop. The trick here is to initialize highScore to a value that is LOWER than any possible score and initialize lowScore to a value that HIGHER than any possible score. Then you update them inside the loop with:
1
2
if (judgeScore < lowScore) lowScore = judgeScore;
if (judgeScore > highScore) highScore = judgeScore;


Line 46-54 should come before you do the calculations. There's no sense doing the calculations if the score is bad.

What are you trying to do at lines 49-51? The code you have is equivalent to --judgeNumber;

You probably want to insert continue; after line 53 to get it to go back to the top of the loop.

Move line 57 to after line 58. THere's no sense computing the grossScore until you have all the judges' scores.
Thanks a lot for your help. Could you hint why every time after the compiler goes through the first iteration of the for loop in lines (24-30), anytime after that it does not allow you to input a diver's name? The command prompt puts lines 26 and 29 on the same line, and doesn't allow the user to input another divers name.
Last edited on
So far I can tell you that you are adding 1 to i if selecting yes to add another, AND you have in the for expression to increment i again.

So you start out with i being 0. If you enter the first diver and opt to add another, i+=2. That is fine, but if you add a third, i is now 4. Then 6, 8, etc.

I tried to display the results with only the first diver, and it ended up dividing by 0, so it gave me a funky figure for average. This happened because divers and allDivers I found came out to 0 with only the first diver. In the code, it is because in line 100, you have if (i=0) should just be if (i == 0).

As I suspected, the skipover has to do with the way getline works. Apparently, from what I can gather (beginner here), since you are using cin later in the code, the newline is still sort of there. cin itself seems to automatically skip over a newline character, but getline accepts it. In other words, it looks like it is working like this in your code.

INPUT STREAM...........FUNCTION
butch thompson\n......getline using cin to save in name, minus the newline character
orlando\n..................." ", but to save in city
1\n............................cin, ignores the newline character (doesn't get processed)
\n2\n.........................cin, ignores the previous newline character, saves the number, skips the second newline character?
\n3\n........................." "
\n4\n
\n5\n
\n1.00\n
\ny\n.........................cin, ignores the previous newline character, saves the char, skips the second newline character
\n..............................getline, giving you the same result as if you were to press enter the first go
nextCity\n.................getline functioning properly.


I am probably completely wrong. However, I know for a fact (cause I've tried it) that if you insert the line cin.ignore(); at the end of the loop, it will essentially clear out the \n that it is reading, and it will work fine after that.

cin.ignore() actually does clear out everything in the input stream no matter what it is. Also, I learned there is a cin.clear() function, but it DOES NOT clear out the input stream. It clears something else.
Last edited on
Topic archived. No new replies allowed.