Thread: C++ Data Struct using an array as input

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Post C++ Data Struct using an array as input

    Question:

    How do I get my data struc program to enter input about 12 players. I just can not seem to get it. My code below only enters one player. I think I understand the concept but can't seem to make it work.

    // Write a prg that stores info. about a soccer player in a struc.
    // The program should keep an array of 12 of these structures.
    // Each element is for a different player on a team.
    // When the program runs it should ask the user to enter the info for
    // each player. It should the show a table that lists eahc player's
    // number, name, and points scored.
    // The program should also calculate and display the total points
    // earned by the team.
    // The number and name of player that has earned the most points should also
    // be displayed.


    // My current program only does one person. How do I make the array loop
    //through each element for all 12 players.

    #include <stdio.h>
    #include <iostream.h>

    struct player
    {
    char name[35];
    int no;
    int point;
    };

    // struct player soccer[12]; My array to enter 12 players
    // int i;
    // for (i=0, i<12; i++) My loop to enter 12 player info.

    void getData(player *);

    void main(void)
    {
    player soccer;
    cout<<"Enter player data:\n";
    getData(&soccer);
    cout <<"\n This is what you entered:\n";
    cout.precision(2);

    // now display the data scored in soccer

    cout<<"Name: " << soccer.name <<endl;
    cout <<"Number: "<< soccer.no <<endl;
    cout <<"Point: " << soccer.point << endl;
    }

    // Def of function getData useds a pointer to a player stucture
    // variable. The user enters into, which is stored in the variable

    void getData(player *p)
    {
    cout << "Player's name: ";
    cin.getline(p->name, 35);
    cout <<"Player's number: ";
    cin.ignore (); //ignore the leftover new line.
    cin>>p->no;
    cout <<"Points scored by player: ";
    cin>>p->point;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Code:
    #define NUM_PLAYERS 12
    
    player soccer[NUM_PLAYERS];
    
    for(int i = 0; i < NUM_PLAYERS; i++)
    {
        getData(&soccer[i]);
    }
    
    for(i = 0; i < NUM_PLAYERS; i++)
    {
        cout<<"Name: " << soccer[i].name <<endl;
        cout <<"Number: "<< soccer[i].no <<endl;
        cout <<"Point: " << soccer[i].point << endl;
    }
    hope this helps
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Unregistered
    Guest

    I wonder if.....

    you are using the starting out with C++ book 3rd edition by Tony Gaddis?

    Reminds me of a problem when I took it at a CC in Maine.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    1
    bringing this back from the dead....

    when you compile this you get many errors...how would you fix it so it doesnt have any??

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    struct mystruct
    {
        string players;
    }player[12];
    
    int main()
    {
        cout << "enter info" << endl;
        getline( cin, player[1].players, '\n');
        cout << player[1].players << endl;
        //etc with [1], [2], [3]  so fourth, use a for loops to
        //fill and display them.
    }
    will need to include <string> of course
    and you can add your other elements in the struct wont make a
    different.
    Last edited by ILoveVectors; 07-28-2005 at 07:23 PM.

  6. #6
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    code tags are your friends
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > bringing this back from the dead....
    Then you should learn to read the bloody rules before opening your mouth

    Especially the one about bumping threads.
    Like this one, which has been in the boneyard for 3 years.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM