Code not working

Trying to write a function that inputs the data through use of arrays and can be quit at any time between 1 and 100 (or in array case, 0 to 99). My code doesn't appear to be working, and I'm not quite sure what it is I'm doing wrong. I wrote the errors in comments where they appear.

Thanks in advance for assistance.

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

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

using namespace std;

//main function
int main()
{
	string name[100]; //Array for name entries
	double score[100] = { 0 }; //Array for the scores
	double avgScr = 0; //score average
	int x = 0;
	int totalPlayers = 0; //total number of players
	
	inputData(name[], score[], x); //call to function
}

//Input and Display name and score
void inputData(string name[], double score[], int x) 
{
	for (int x = 0; x <= 99; x++)
	{
		cout << "Player name (Q to quit): "; //name
		cin >> name[x];

		//Quit Options 
		switch (name[x]) 
		{
		case 'Q':
		case 'q': 
			break;

		default: 
			cout << "\nPlayer Score: ";
			cin >> score[x];
		}
	}

}
Last edited on
A break within switch merely jumps out from the switch. It will not terminate the loop.
In addition to what keskiverto stated.

Line 17:
After declaration, "array[n]" is a call to get an element. Just pass pointers:
inputData(name, score, x);
Also, you never declared inputData before calling it, so the compiler won't know what inputData is.

Line 29:
Switch-cases can only be used to compare integral, non-pointer types (because the values for each case must be constant and integral). Also, since you only have one condition to check, a switch-case is overkill.
1
2
3
4
5
6
7
::tolower(name[x].front());
if(name[x] == "q"){
    return; //Exit the function
}else{
    cout << "\nPlayer Score: ";
    cin >> score[x];
}
Last edited on
Thank you both for pointing out my errors! Especially the switch statement and the termination fail.

I did alter my code to match your suggestions, though I had issues making the code you submit (Daleth) work for me.

The only remaining issue I'm having at this moment is this:

1>------ Build started: Project: w5-lab, Configuration: Debug Win32 ------
1>  w5.cpp
1>w5.obj : error LNK2019: unresolved external symbol "void __cdecl displayData(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,double * const,int)" (?displayData@@YAXQAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QANH@Z) referenced in function _main
1>C:\Users\Damir\Documents\Visual Studio 2013\Projects\w5-lab\Debug\w5-lab.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Here's my code, though I don't know what exactly the issue is, or if it's in 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
//--------------------------------------------------------------------------------
//Programming Assignment:  LAB4
//Developer:               Sierra McGivney
//Date Written:            04/02/2014
//Purpose:                 Video Game Player Program
//--------------------------------------------------------------------------------

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


using namespace std;


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

//main function
int main()
{
	string name[100]; //Array for name entries
	double score[100] = { 0 }; //Array for the scores
	double avgScr = 0; //score average
	int x = 0;
	int totalPlayers = 0; //total number of players

	//call functions
	inputData(name, score, x); //input
	displayData(name, score, x); //ouput
}

//Input and Display name and score
void inputData(string name[], double score[], int x)
{
	//declarations
	int count = 0;
	double scoreTot = 0.0;

	//loop
	for (int x = 0; x <= 99; x++)
	{
		cin.sync(); //If I don't include this, my loop will not allow me to enter a new name, and skips right to score. 
		cout << "\nPlayer name (Q to quit): "; //name
		getline(cin, name[x]);

		//Quit Options 
		if ((name[x] == "Q") || (name[x] == "q"))
		{
			return; //Exit the function
		}
		else
		{
			cout << "\nPlayer Score: "; //score
			cin >> score[x];

			count++;
		}
	}//end for loop
}//end function


void displayData(string name[], double score[], int x, int count, double scoreTot)
{
	for (x = 0; x <= 99; x++)
	{
		//Display Output
		cout << "\tName\tScore" << endl;
		cout << name[x] << "\t\t" << score[x] << endl;
	}//end for loop

	//calc Avg
	double scrAvg = 0.0;
	scrAvg = scoreTot / count;

	//display avg
	cout << "\nThe score average was: " << scrAvg << endl;
}//end function 
Your function prototype for displayData at the top (line 18) is different to the displayData function. at line 64
Okay, solved that error, never mind. But, when I run the program, only two names show up in below average when three should. Not sure why it's not working.

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 <<- this is what I need!
Press any key to continue . . .


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
//--------------------------------------------------------------------------------
//Programming Assignment:  LAB4
//Developer:               Sierra McGivney
//Date Written:            04/02/2014 - 04/04/2014
//Purpose:                 Video Game Player Program
//--------------------------------------------------------------------------------

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

using namespace std;


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

//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, highest, scoreTot); //input
	if (count != 0)
	{
		displayData(name, score, x, count, scoreTot, highest); //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, int &highest, double &scoreTot)
{

	//loop
	for (int x = 0; x <= 99; x++)
	{
		cin.sync(); //If I don't include this, my loop will not allow me to enter a new name, and skips right to score. 
		cout << "Player name (Q to quit): "; //name
		getline(cin, name[x]);
		
		//Quit Options 
		if ((name[x] == "Q") || (name[x] == "q"))
		{
			highest = count;
			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, int &highest)
{
	//declarations
	double scrAvg = 0.0;

	cout << setw(8) << "\n\tName\t\tScore" << endl;
	for (x = 0; x <= highest; 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 <= highest; 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 
Last edited on

The problem with your code is the break statement used inside the for..loop of displayData - as soon as it hits a score that is above average its going to break out of the loop and skip the rest.
Topic archived. No new replies allowed.