Thread: Trying to get it to name player who score the most

  1. #1
    Unregistered
    Guest

    Unhappy Trying to get it to name player who score the most

    points. Got a hang-up somewhere. Please help.
    Does the rest fine except for the last requirement and that is to name the player who scored the most pts.

    Thanks,

    #include <iostream.h>
    #include <stdlib.h>

    struct PlayerInfo
    {
    char name[25];
    int playerNum;
    int pointsScored;
    };

    void main(void)
    {
    PlayerInfo players[12]; //array of 12 structs

    int number = 0;
    int index = 0;
    int totalPoints = 0;
    char y;

    cout << "Soccer Scores" << endl << endl;
    cout << "How many players do you want to enter? ";
    cin >> number;
    cout << endl;
    for (index = 0; index < number; index++)
    {
    cout << "Player #" << (index + 1) << "\n--------------\n";
    cout << "Player's name: ";
    cin.ignore();
    cin.getline(players[index].name, 25);
    cout << "Player's number: ";
    cin >> players[index].playerNum;
    while (players[index].playerNum < 0)
    {
    cout << "Please enter a positive number for player's number ";
    cin >> players[index].playerNum;
    }
    cout << "Points scored: ";
    cin >> players[index].pointsScored;
    while (players[index].pointsScored < 0)
    {
    cout << "Please enter a positive number for points scored ";
    cin >> players[index].pointsScored;
    }
    cout << endl;
    }

    cout << "NAME\t" << "NUMBER\t" << "POINTS SCORED\n";
    for (index = 0; index < number; index++)
    {
    cout << players[index].name << "\t" << players[index].playerNum
    << "\t" << players[index].pointsScored << endl;
    totalPoints = totalPoints + players[index].pointsScored;
    }
    cout << "TOTAL POINTS: " << totalPoints << endl;

    int temp;
    for (index = 0; index < number; index++) // sort to find highest score
    for ( int y = 0; y < number; y++)
    {
    if (players[y].pointsScored > players[index].pointsScored)
    {
    temp = players[index].pointsScored;
    players[index].pointsScored = players[y].pointsScored;
    players[y].pointsScored = temp;

    }
    }

    //How do I convert highest score into name who had highest score

    for(index +1 + number; index <= number; index++)
    {
    cout << "The player who scored the most points is: " << players[index].name;
    }
    cout << endl;

    system("PAUSE");

    }

  2. #2
    Unregistered
    Guest
    if your sort routine sorts in ascending order and number is the last index used in the array, then the name in the struct with the last index is the player with the most points. One problem, I think the syntax for your sort routine is off. I suggest looking up code for bubble sort, if you want to do it that way. The other way is assign the value of score in the first element to a dummy variable. Then compare that variable with every other value in the array, replacing the dummy value with each successive value if appropriate. At the end, that will be the hihest score, and that index will hold the name.

  3. #3
    Unregistered
    Guest

    Smile I like your idea much better. Thanks.

    The bubble seems like it would be overkill. Your idea makes better sense.

  4. #4
    Unregistered
    Guest

    Thumbs up Got it to work. Thanks for the direction.

    Sometimes all it takes is to be pointed down the right path.

    Thanks dude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  4. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 06:44 PM
  5. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM