Im getting this error and I am so new at this that I have no idea how to correct it.
If someone has any suggestions.
Programming for 10 weeks!

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int InputData(string [], int []);
int main()
{
	string players[99];
	int score[99];
	int numPlayers = 0;
	double averageScore = 0;
	numPlayers = InputData (players, score);
	for (int i = 0; i < numPlayers; i++)
	{
		cout << players[i] << ": " << score[i] << endl;
	}
	return 0;
}
int InputData(string playerName[], int score[])
{
	for (int i = 0; i < 100; i++)
	{
		cout << "Enter the players name (Q to quit): ";
		getline(cin, playerName[i], '\n');

		if ((playerName[i] == "Q") || (playerName[i] =="q"))
		{
			return i;

		}

		cout << "Enter the score of the player " << playerName[i] << ": ";
		cin >> score[i];
		cin.ignore();
	}
}

void DisplayPlayerData( string playerName[], int score[], int numPlayers )
{
	// Display the output header column names
	cout << "  Name   Score" << endl;

	// Loop through all the players in the array and print their name and score
	for (int i = 0; i < numPlayers; i++)
	{
		cout << playerName[i] << "   " << score[i] << endl;
	}
}
// Calculate Average Score
// Returns: the average score as an integer
//
double CalculateAverageScore( int score[], int numPlayers )
{
	double scoreTotals = 0;

	// Loop through all the players in the array and print their name and score
	for (int i = 0; i < numPlayers; i++)
	{
		scoreTotals += score[i];   // Sum up the total scores and keep them in the running value of scoreTotals
	}
	// Display the Average Score which is the total sum of scores divided by the number of players 
	cout << endl << "Average Score:  " << scoreTotals / numPlayers << endl;

}

//
// Display Below Average scores
// Returns: nothing (void)
//
void DisplayBelowAverage( string playerName[], int score[], int numPlayers, int averageScore  )
{
	// Display the output header column names
	cout << endl << "Players that scored below average" << endl << "  Name   Score" << endl;

	// Loop through all the players in the array and conditionally print their name and below saverage score
	for (int i = 0; i < numPlayers; i++)
	{
		if( score[i] < averageScore ) 
		{
			cout << playerName[i] << "   " << score[i] << endl;
		}
	}
}

Dani AI

Generated

The compiler error came from a non-void function finishing without a return value: the original CalculateAverageScore was declared to return double but had no return. correctly flagged the missing return; suggested making the function void (that removes the compile error but also discards the computed average). ’s later change to return an int does compile, but it truncates fractional averages and leaves a few edge cases unhandled.

A safer approach is to return a double, guard against zero players, and avoid integer truncation. The example below shows a compact, robust average function that is not in the thread’s posts:

double CalculateAverageScore(const int scores[], int numPlayers) {
    if (numPlayers <= 0) return 0.0;            // avoid divide-by-zero
    long long total = 0;
    for (int i = 0; i < numPlayers; ++i) total += scores[i];
    return static_cast<double>(total) / static_cast<double>(numPlayers);
}

Other practical suggestions tied to the thread:

  • Ensure InputData always returns an integer when the loop finishes (add a return after the loop or structure the loop so a final return is inevitable). Falling off the end of a non-void function causes undefined behavior.
  • Keep array sizes consistent with loops (the original code used mismatched sizes such as 99 vs loops to 100). Prefer a const int MAX_PLAYERS or std::vector to avoid magic numbers.
  • Decide whether the average should be fractional; if so, make DisplayBelowAverage accept a double (or compare with an explicitly rounded threshold) to avoid subtle off-by-one comparisons caused by truncation.
  • Be careful mixing std::getline and operator>>: either consume the leftover newline reliably or read the number as a string and parse it with std::stoi, and always check cin.fail() after formatted input.

These points preserve the fix (return a value) while improving correctness and robustness beyond the compile-time change.

Recommended Answers

All 3 Replies

As it says you have a double function so it is expecting a double value to be returned.

Plus you seem to be missing some function definitions.

Line 52. It must be a void instead of a double.

Here is the correct code fixed:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int InputData(string [], int []);
void DisplayPlayerData( string [], int [], int );
int CalculateAverageScore( int [], int ); 
void DisplayBelowAverage( string [], int [], int, int );

int main()
{
	string players[100];
    int score[100];
    int numPlayers = 0;
    int averageScore = 0;

    numPlayers = InputData (players, score);
      
    //
    // Display the players and their scores
    //
    DisplayPlayerData( players, score, numPlayers );

    //
    // Call CalculateAverageScore() which returns thre score average as an integer
    // Note that we only pass the variables we actually need in the function - not the ones we do not need
    //
    averageScore = CalculateAverageScore( score, numPlayers );

    //
    // Display the below average scores
    //
    DisplayBelowAverage( players, score, numPlayers, averageScore );

    return 0;

	
}
int InputData(string playerName[], int score[])
{
	for (int i = 0; i < 100; i++)
	{
		cout << "Enter the players name (Q to quit): ";
		getline(cin, playerName[i], '\n');

		if ((playerName[i] == "Q") || (playerName[i] =="q"))
		{
			return i;

		}

		cout << "Enter the score of the player " << playerName[i] << ": ";
		cin >> score[i];
		cin.ignore();
	}
}

void DisplayPlayerData( string playerName[], int score[], int numPlayers )
{
	// Display the output header column names
	cout << "  Name   Score" << endl;

	// Loop through all the players in the array and print their name and score
	for (int i = 0; i < numPlayers; i++)
	{
		cout << playerName[i] << "   " << score[i] << endl;
	}
}
// Calculate Average Score
// Returns: the average score as an integer
//
int CalculateAverageScore( int score[], int numPlayers )
{
	int scoreTotals = 0;
	
	// Loop through all the players in the array and print their name and score
	for (int i = 0; i < numPlayers; i++)
	{
		scoreTotals += score[i];   // Sum up the total scores and keep them in the running value of scoreTotals
	}
	// Display the Average Score which is the total sum of scores divided by the number of players 
	cout << endl << "Average Score:  " << (float)scoreTotals / (float)numPlayers << endl;
	return scoreTotals / numPlayers;
}


// Display Below Average scores
// Returns: nothing (void)
//
void DisplayBelowAverage( string playerName[], int score[], int numPlayers, int averageScore  )
{
	// Display the output header column names
	cout << endl << "Players that scored below average" << endl << "  Name   Score" << endl;

	// Loop through all the players in the array and conditionally print their name and below saverage score
	for (int i = 0; i < numPlayers; i++)
	{
		if( score[i] < averageScore ) 
		{
			cout << playerName[i] << "   " << score[i] << endl;
		}
	}
}
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.