I'm having trouble getting the player's name and score to display as well as the average score. I have been at this for about 3 hours. I think it's time for a fresh set of eyes. Can someone please help?! This assignment is due tomorrow. Thanks

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

using namespace std;

//prototypes
int inputData(string[], int[], int);
void displayData(string[], int[], int);
int calculateAverageScore(int [], int, int);
void displayBelowAverage(string[], int[], int, int);


int main()
{
	string playerName[100];
	int playerScore[100];
	int average = 0;
	int numOfPlayers = 0;
	
	
	//call functions
	inputData (playerName, playerScore, numOfPlayers);
	displayData(playerName, playerScore, numOfPlayers);
	calculateAverageScore(playerScore, numOfPlayers, average);
	displayBelowAverage(playerName, playerScore, numOfPlayers, average);

	return 0;
}

int inputData(string playerName[], int playerScore[], int numOfPlayers)
{
	for (int i = 0; i < 100; i++)
	{
		cout << "Enter player's name (Q to quit): ";
		getline(cin, playerName[i], '\n');
		
		if ((playerName[i] == "Q") || (playerName[i] == "q"))
		{
			return i;
		}
	
		cout << "Enter score for " << playerName[i] << ": ";
		cin >> playerScore[i];
		cin.ignore();
		
		numOfPlayers += 1;
	}// end while
}


void displayData(string playerName[], int playerScore[], int numOfPlayers)
{
	cout << " Name    Score " << endl << endl;

	for (int i = 0; i < numOfPlayers; i++)
	{
		cout << playerName[i] << "    " <<  playerScore[i] << endl;
	}
}

int calculateAverageScore(int playerScore[], int numOfPlayers, int average)
{	
	for (int i = 0; i < numOfPlayers; i++)
	{
		average += playerScore[i];
		average = average / numOfPlayers;
	}
		
	cout << endl << "Average Score: " << average << endl;
	return average;
}

void displayBelowAverage(string playerName[], int playerScore[], int numOfPlayers, int average)
{
	int belowAverage = 0;

	cout <<endl << "Players that scored below average" << endl << " Name    Score" << endl;

	for (int i = 0; i < average; i++)
	{
		if (playerScore[i] < average)
			playerScore[i] = belowAverage;
		{
			cout << playerName[i] << "    " << playerScore[i] << endl << endl;
		}
	}
}

I'm also getting this warning message: 1>c:\users\nika\documents\visual studio 2008\projects\lab5a\lab5a\lab5a.cpp(55) : warning C4715: 'inputData' : not all control paths return a value.

Recommended Answers

All 6 Replies

Well, if you put the call to displayData inside your input after your loop, then that part will work fine. You are getting the warning because you have no default return value. Just put return 0; at the bottom of your function.

Is there anyway you can give me an example? I'm new to programming and still alittle unsure about the lingo.

int inputData(string playerName[], int playerScore[], int numOfPlayers)
{
	for (int i = 0; i < 100; i++)
	{
		cout << "Enter player's name (Q to quit): ";
		getline(cin, playerName[i], '\n');
		
		if ((playerName[i] == "Q") || (playerName[i] == "q"))
		{
			return i;
		}
	
		cout << "Enter score for " << playerName[i] << ": ";
		cin >> playerScore[i];
		cin.ignore();
		
		numOfPlayers += 1;
	}// end while
//Call your displayData here
displayData(playerName, playerScore, numOfPlayers);

//You need to specify a default return statement, just incase none of your code gets executed, etc..
return 0;
}

Oh...Okay...That's what I thought. Thank you. Now I just got to figure out some of the other issues.

So here is my updated code. However, I still can't get it to calculate and output the average score nor does it display the below average information. Can somebody help please?! It due today.

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

using namespace std;

//prototypes
int inputData(string[], int[], int);
void displayData(string[], int[], int);
int calculateAverageScore(int [], int, int, int);
void displayBelowAverage(string[], int[], int, int);

int numOfPlayers = 0;

int main()
{
	string playerName[100];
	int playerScore[100];
	int average = 0;
	int totalScore = 0;
	
	
	//call functions
	inputData (playerName, playerScore, numOfPlayers);
	
	calculateAverageScore(playerScore, numOfPlayers, average, totalScore);
	
	displayBelowAverage(playerName, playerScore, numOfPlayers, average);
}

int inputData(string playerName[], int playerScore[], int numOfPlayers)
{
	for (int i = 0; i < 100; i++)
	{
		cout << "Enter player's name (Q to quit): ";
		getline(cin, playerName[i], '\n');
		
		if ((playerName[i] == "Q") || (playerName[i] == "q"))
		{
			//return i;
			break;
		}
		else
		{
		
			cout << "Enter score for " << playerName[i] << ": ";
			cin >> playerScore[i];
			cin.ignore();
			numOfPlayers += 1;
		}
	}// end while
	
	//call display function
	displayData(playerName, playerScore, numOfPlayers);	
	
	return 0;
}



void displayData(string playerName[], int playerScore[], int numOfPlayers)
{
	cout << " Name    Score " << endl << endl;
	
	for (int i = 0; i < numOfPlayers; i++)
	{
		cout << playerName[i] << "    " <<  playerScore[i] << endl;
	}
	
}

int calculateAverageScore(int playerScore[], int numOfPlayers, int average, int totalScore)
{	
	for (int i = 0; i < numOfPlayers; i++)
	{
		totalScore += playerScore[i];
		average = totalScore / numOfPlayers;
	}
		
	cout << endl << "Average Score: " << average << endl;
	return average;
}

void displayBelowAverage(string playerName[], int playerScore[], int numOfPlayers, int average)
{
	
	cout << endl << "Players that scored below average\n" << endl << " Name    Score\n" << endl;
	
	//for (int i = 0; i < average; i++)
	for (int i = 0; i < numOfPlayers; i++)
	{
		if (playerScore[i] < average)
			//playerScore[i] = belowAverage;
		{
			cout << playerName[i] << "    " << playerScore[i] << endl ;
		}
	}
	
}

I am working on the same assignment...and I am just as lost as you! Although you seem to be farther along than me. I started one..got a good way through it, but ended up with too many errors..so decided to start completely over..and now I am trying a different route..only I am stuck at the beginning! LOL...good luck and hope you get it figured out..but if you don't, you will get partial credit. I just wish they'd give you a completed code..so you can see where you messed up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.