I am a beginning at programming and need to know what I am doing wrong with this code. I keep getting error codes on undeclared variables, storage types, etc.

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

const int ARRAY_SIZE = 100;
void InputData(string, int[], int &);
void DisplayPlayerData(const string, const int[], int);
double CalculateAverageScore(const int[], int);
void DisplayBelowAverage( const string; const int[]; int, double);

void main()
{
//Declare the player name and score arrays, number of players, and average score.
string playerNameAr[ARRAY_SIZE];
int scoreAr[ARRAY_SIZE];
int numPlayersAr = 0;
double averageScore;

cout << fixed << showpoint << setprecision(2);

//Call the InputData function
InputData(playerNameAr, scoreAr, numPlayersAr);
//Call the DisplayPlayerData function
DisplayPlayerData(playerNameAr, scoreAr, numPlayersAr);
//Call the CalculateAverageScore function and assign the returned value in average score
CalculateAverageScore(scoreAr, numPlayersAr);
//Call the DisplayBelowAverage function
DisplayBelowAverage(playerNameAr, scoreAr, numPlayersAr, averageScore);

}
void InputData(playerNameAr[], scoreAr[], numPlayersAr);
{
while(numPlayersRef < ARRAY_SIZE)
{
//Prompt for the player's name or Q to quit
cout << "Enter the player's name (Q to quit). " << endl;
getline (cin, playerNameAr[numPlayersRef]);
if (playerNameAr[numPlayersRef] == "Q") break;
//Prompt the user for the player's score
cout << "Enter the player's score. " << endl;
cin >> scoreAr[i];
//Add 1 to the number of players 
numPlayersRef++;
}
}
void DisplayPlayerData(playerNameAr[], scoreAr[], numPlayers)
{
cout << setw(10) << left << "\n Name" 
<< setw(5) << right << "Score" << endl;
for(int i = 0; i < numPlayers; i++)
{ 
//Display the name and score of each player
cout << "The players name is: " << playerNameAr[i] << endl;
cout << "The players score is: " << scoreAr[i] << endl;
i++;
}
} 
double CalculateAverageScore(scoreAr[], numPlayers)
{
int i;
double averageScore, totalScore;

for(i = 0, totalScore = 0; i < numPlayers; i++)
{
//Add up the scores 
totalScore += scoreAr[i];
}
//divide by the number of scores to calculate the average score
averageScore = totalScore / numPlayers;
//Display the average score
cout << "Average score is ." << averageScore << endl;
//Return the average score to main
return averageScore;


}
void DisplayBelowAverage(playerNameAr[], scoreAr[], numPlayers, averageScore)
{
cout << "Players who scored below average\n";
cout << setw(10) << left << " Name" << setw(5) << right << "Score" << endl;
for(int i = 0; i < numPlayers; i++)
if(scoreAr[i] < averageScore)
//Display the names and scores of all players who scored below the average score
cout << setw(10) << left << playerNameAr[i] << setw(5) << right << scoreAr[i] << endl;
}

Recommended Answers

All 9 Replies

You have the following errors.

1) No using namespace std or std:: (for cout, string etc)

2) void InputData(string, int[], int &);
This function seems to take a string, but you are passing a string array here:
InputData(playerNameAr, scoreAr, numPlayersAr); --> Line 25

3) Line 34 -> There is a ; found in the function definition.

void main()

Use int main() or int main(int argc, char *argv[]) as your entry-point, "void main" is not and never has been valid C or C++.

void DisplayBelowAverage( const string; const int[]; int, double);

replace ';' with ','.

void DisplayPlayerData(playerNameAr[], scoreAr[], numPlayers)

that is not valid as a function header!


Take this for example:

#include <iostream>
#include <string>
using namespace std;

//Function prototype/declaration.
void PrintSomething(string[], int[], int);

int main()
{
   string stringArray[128];
   int intArray[128] = {0};
   PrintSomething(stringArray,intArray,1000);
}

//Function definition.
void PrintSomething(string someLocalFunctionName[], int otherFunctionLocalName[], int n)
{
  if(someLocalFunctionName != NULL)
    ; //etc.
}

"void main" is not and never has been valid C

Actually, the ISO C spec allows void main()

The opinion of Stroustup pales in comparison to the actual C definition.

There is no need to appeal to authority when you can read the words yourself. Section 5.1.2.2.1 of the actual C99 definition closes "or in some other implementation-defined manner" which allows for void main()

Here is a simple breakdown of it: http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html

The opinion of Stroustup pales in comparison to the actual C definition.

There is no need to appeal to authority when you can read the words yourself. Section 5.1.2.2.1 of the actual C99 definition closes "or in some other implementation-defined manner" which allows for void main()

Here is a simple breakdown of it: http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html

The thing you need to remember is that "implmentation-defined manner" is synonymous with "compiler-specific behavior"; so you've pretty-much just hung yourself.

Yes, void main() is an allowed definition, but it's not a specifically-recognized, and therefore not a portable, definition. One of the major goals of C/C++ is portability. The only universally-accepted (portable) implementations per The Standards are int main() and int main(int argc, char *argv[]) because they are specifically required/recognized by The Standards.

Any other version must be considered a compiler-specific extension. You may as well be parroting "It works for me!". Sure, your compiler may accept it, but if you want me to look at your code you have to assume that my compiler won't, because you don't know what compiler I'm using and it may not support that specific extension to the language.

EDIT:
FYI - I've been avoiding posting in this thread. But, now I think it's time to diffuse this conversation and get the thread back on topic.

@OP:
Your biggest issue is not resolving the std namespace for any of the Library functions. You need to add some sort of resolution to your code. You can do a general resolution (not generally recommended, but common for "newbies"):

#include <iostream>

using namespace std;  //resolve the entire std namespace

int main() {
  /*...*/
}

Identifier-specific resolutions:

#include <iostream>

using std::cout;  //resolve the cout identifier
using std::endl;  //resolve the endl identifier

int main() {
  /*...*/
}

Or in-line resolutions:

#include <iostream>

int main() {
  std::cout << "blah" << std::endl;  //resolve the identifiers in-line
  /*...*/
}
commented: A mini-lecture that missed the point entirely -1
commented: Good, provides clarity. +9

diffuse

I expect you meant defuse :) I know, I just can't stop.

diffuse

I expect you meant defuse :) I know, I just can't stop.

No he surely intended to add a haze of fog to our stupidity as that's the only solution.
Now it's just *Everywhere* =D

Getting back to the original question, the code below fixes most of the syntactic errors in the code, though I did not attempt to correct the logical flaws without a clearer idea of the intended behavior:

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

using namespace std;

const int ARRAY_SIZE = 100;

void InputData(string[], int[], int &);
void DisplayPlayerData(const string[], const int[], int);
double CalculateAverageScore(const int[], int);
void DisplayBelowAverage( const string[], const int[], int, double);

int main()
{
//Declare the player name and score arrays, number of players, and average score.
    string playerNameAr[ARRAY_SIZE];
    int scoreAr[ARRAY_SIZE];
    int numPlayersAr = 0;
    double averageScore;

    cout << fixed << showpoint << setprecision(2);

    //Call the InputData function
    InputData(playerNameAr, scoreAr, numPlayersAr);
    //Call the DisplayPlayerData function
    DisplayPlayerData(playerNameAr, scoreAr, numPlayersAr);
    //Call the CalculateAverageScore function and assign the returned value in average score
    averageScore = CalculateAverageScore(scoreAr, numPlayersAr);
    //Call the DisplayBelowAverage function
    DisplayBelowAverage(playerNameAr, scoreAr, numPlayersAr, averageScore);

    return 0;
}


void InputData(string playerNameAr[], int scoreAr[], int & numPlayersRef)
{
    while(numPlayersRef < ARRAY_SIZE)
    {
        //Prompt for the player's name or Q to quit
        cout << "Enter the player's name (Q to quit). " << endl;
        getline (cin, playerNameAr[numPlayersRef]);
        if (playerNameAr[numPlayersRef] == "Q") break;
        //Prompt the user for the player's score
        cout << "Enter the player's score. " << endl;
        cin >> scoreAr[numPlayersRef];
        //Add 1 to the number of players
        numPlayersRef++;
    }
}

void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers)
{
    cout << setw(10) << left << "\n Name"
         << setw(5) << right << "Score" << endl;
    for(int i = 0; i < numPlayers; i++)
    {
        //Display the name and score of each player
        cout << "The players name is: " << playerNameAr[i] << endl;
        cout << "The players score is: " << scoreAr[i] << endl;
        i++;
    }
}

double CalculateAverageScore(const int scoreAr[], int numPlayers)
{
    int i;
    double averageScore, totalScore;

    for(i = 0, totalScore = 0; i < numPlayers; i++)
    {
        //Add up the scores
        totalScore += scoreAr[i];
    }
    //divide by the number of scores to calculate the average score
    averageScore = totalScore / numPlayers;
    //Display the average score
    cout << "Average score is ." << averageScore << endl;
    //Return the average score to main
    return averageScore;
}

void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore)
{
    cout << "Players who scored below average\n";
    cout << setw(10) << left << " Name" << setw(5) << right << "Score" << endl;
    for(int i = 0; i < numPlayers; i++)
        if(scoreAr[i] < averageScore)
            //Display the names and scores of all players who scored below the average score
            cout << setw(10) << left << playerNameAr[i] << setw(5) << right << scoreAr[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.