Something wrong with my code?

This is the code:
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

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


//functions
int inputData(string name[], double score[], int x, int &count, double &scoreTot);
void displayData(string name[], double score[], int x, int &count, double &scoreTot);

//main function
void main()
{
	string name[100]; //Array for name entries
	double score[100] = { 0 }; //Array for the scores
	double scrAvg = 0.0; //score average
	int x = 0;
	int highest = 0; //use this to dictate where to stop in the following function
	int count = 0; //total number of players
	double scoreTot = 0.0;

	//call functions
	inputData(name, score, x, count, scoreTot); //input
	if (count != 0)
	{
		displayData(name, score, x, count, scoreTot); //ouput function
	}//end if loop
	return;
}//end main function
	

//Input and Display name and score
int inputData(string name[], double score[], int x, int &count, double &scoreTot)
{

	//loop
	for (int x = 0; x <= 99; x++)
	{
		cin.sync();
		cout << "Player name (Q to quit): "; //name
		getline(cin, name[x]);
		
		//Quit Options 
		if ((name[x] == "Q") || (name[x] == "q"))
		{
			return count; //Exit the function
		}
		else
		{
			count =  count + 1;
			cout << "Player Score: "; //score
			cin >> score[x];
			while ((score[x] < 0.0) || (score[x] > 9999))
			{
				//enter score
				cout << "Error: Score must be between 0 and 9999, in numeric form." << endl;
				cout << "Enter score: ";
				cin >> score[x];
				cout << "\n";
			}//end while loop
			scoreTot = scoreTot + score[x];
			cout << "\n";
		}//end else if
	}//end for loop
}//end function


void displayData(string name[], double score[], int x, int &count, double &scoreTot)
{
	//declarations
	double scrAvg = 0.0;

	cout << setw(8) << "\n\tName\t\tScore" << endl;
	for (x = 0; x <= count; x++)
	{
		//Display Output
		if ((name[x] == "Q") || (name[x] == "q"))
		{
			break;
		}
		else
		{
			cout << setw(8) << "\t" << name[x] << "\t\t" << score[x] << endl;
		}
	}//end for loop

	scrAvg = scoreTot / count; //calc Avg

	//display avg
	cout << "\nThe score average was: " << scrAvg << endl;
	
	//below avg
	cout << "\nPlayers who scored below average:" << endl;
	cout << setw(8) << "\n\tName\t\tScore" << endl;
	for (x = 0; x <= count; x++)
	{
		//Display Output
		if (score[x] <= scrAvg)
		{
			cout << setw(8) << "\t" << name[x] << "\t\t" << score[x] << endl;
		}
		else
		{
			break; 
		}//end else if

	}//end for loop
}//end function


This is the desired output:
Enter Player Name (Q to quit): Bob
Enter score for Bob: 3245
Enter Player Name (Q to quit): Sue
Enter score for Sue: 1098
Enter Player Name (Q to quit): Dave
Enter score for Dave: 8219
Enter Player Name (Q to quit): Pat
Enter score for Pat: 3217
Enter Player Name (Q to quit): Q

Name Score
Bob 3245
Sue 1098
Dave 8219
Pat 3217

Average Score: 3944.75

Players who scored below average
Name Score
Bob 3245
Sue 1098
Pat 3217 
Press any key to continue . . .



This is what I get:
Enter Player Name (Q to quit): Bob
Enter score for Bob: 3245
Enter Player Name (Q to quit): Sue
Enter score for Sue: 1098
Enter Player Name (Q to quit): Dave
Enter score for Dave: 8219
Enter Player Name (Q to quit): Pat
Enter score for Pat: 3217
Enter Player Name (Q to quit): Q

Name Score
Bob 3245
Sue 1098
Dave 8219
Pat 3217

Average Score: 3944.75

Players who scored below average
Name Score
Bob 3245
Sue 1098
Press any key to continue . . .
Last edited on
97
98
99
100
101
102
103
104
105
106
107
108
109
	for (x = 0; x <= count; x++)
	{
		//Display Output
		if (score[x] <= scrAvg)
		{
			cout << setw(8) << "\t" << name[x] << "\t\t" << score[x] << endl;
		}
		else
		{
			break; //would terminate the loop
		}

	}
as soon as you find an score above the average, you stop the traverse.

Also you may try to access score[count]
Last edited on
Oh goodness! That was very silly of me! Thank you very much for your response, and also for your pointer. That would have made this a whole lot easier!
Topic archived. No new replies allowed.