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;
}