我的程序在下面。出于某种原因,当我输入我的第一个球员名字(应该是“佩雷斯,胡安”的形式,佩雷斯是姓氏)时,它吓坏了,完成了程序,并为所有人输入了非常大和小的数字我的数字并且不从我的结构数组中询问任何其他信息。现在有没有人对此的推理?
/*
Program will store the information of players of a basketball league
for 10-12 years. Each player will have a name, number, and the points earned
during the game for which they are being monitored. The user of the program will
enter the stats of the player. The players and their stats
will then be printed three times (team has up to ten players) one being sorted
by name, one by number, and another by points during the game.
input:number of players(up to ten) name of each player number of each player
and number of points scored during the game of each payer
output:The name of each player and the number and number of points scored by each player
once sorted by name, another time sorted by number, and another sorted by number of points
scored during the game.
processing: using an abstract data type (structure) the info of each player will be stored
one function will be used to enter the data, another will be used to print an array of players
a third function will be used to sort the players by name, the fourth function will sor the
players by number, and the fifth function will sort the players by number of points scored.
The sixth function will calculate the number of points scored during the game.
*/
#include<iostream>
#include<string>
using namespace std;
struct info
{
string name;
int number;
int points;
};
info enter();
void prinfo(info[], int);
void sortname(info[], int);
void sortnumber(info[], int);
void sortpoints(info[], int);
int total(info[], int);
int main()
{
info player[10];
int numbplay;
int x;
cout<<"How many players were at today's game. ";
cin>>numbplay;
for(int i=0; i<numbplay; i++)
{
player[i]=enter();
}
sortname(player, numbplay);
cout<<"The players at today's game were.\n";
prinfo(player, numbplay);
sortnumber(player,numbplay);
cout<<"After sorting by number the players at today's game were.\n";
prinfo(player, numbplay);
sortpoints(player,numbplay);
cout<<"After sorting by points.\n";
prinfo(player, numbplay);
x=total(player, numbplay);
cout<<"A total of "<<x<<" points were scored during the game";
return 0;
}
/*This function should have no formal parameters, but
will prompt the user of the program to enter the info of
the structure data type array from main and then will return
this back to the main function.
input:name number and points scored during the game for a
specific player
output:returned is the information inputted
processing:simple cin and cout statements
*/
info enter()
{
info person;
cout<<"Enter the name of the player last name first. ";
cin>>person.name;
cout<<"Enter the number for "<<person.name<<" ";
cin>>person.number;
cout<<"How many points did "<<person.name<<" score? ";
cin>>person.points;
return person;
}
/*This function will accept an array (of structure data type) and then print
the elements of the array
input:take from the main, an array of struct data type
output:the elements of the array and the elements of the struct data type
processing:using simple for loop to output the elements
*/
void prinfo(info member[], int amount)
{
cout<<"Name\t\t\t\t\tNumber\t\tPoints";
for(int j=0; j<amount;j++)
{
cout<<member[j].name<<"\t\t\t\t\t"<<member[j].number;
cout<<"\t\t"<<member[j].points<<endl;
}
}
/*This function will sort the array of struct data type by name
input:taken from the main, the array of struct data type
output:none is outputted (sorted array will be given to main)
processing:using the compare string built in function and a simple for loop, and using
selection
sort and simple if statement
*/
void sortname(info people[], int quantity)
{
string temp;
int passcount, placecount,minIndex;
for (passcount=0; passcount<quantity-1; passcount++)
{
minIndex = passcount;
for (placecount = passcount + 1; placecount < quantity; placecount++)
{
if (people[placecount].name.compare(people[minIndex].name) < 0)
minIndex = placecount;
}
temp = people[minIndex].name;
people[minIndex].name = people[passcount].name;
people[passcount].name= temp;
}
}
/*This function will accept the array of struct data type and then the number
of items in that array. The function will then sort the array by number
using selection sort.
Input:array from main
output:no output but array sent back to main
processing: using selection sort the array will be sorted by number,
which contains nested loops, and a simple if
*/
void sortnumber(info contenders[], int value)
{
int useless;
int count, countval, minVal;
for (count=0; count<value-1; count++)
{
minVal = count;
for (countval = count + 1; countval < value; countval++)
{
if (contenders[countval].number < contenders[minVal].number)
minVal = countval;
}
useless = contenders[minVal].number;
contenders[minVal].number = contenders[count].number;
contenders[count].number= useless;
}
}
/*This function will accept the array of struct data type and then the number
of items in that array. The function will then sort the array by points scored
using selection sort.
Input:array from main
output:no output but array sent back to main
processing: using selection sort the array will be sorted by number,
which contains nested loops, and a simple if
*/
void sortpoints(info warriors[], int multitude)
{
int nouse;
int passval, placeval, smallInd;
for (passval=0; passval<multitude-1; passval++)
{
smallInd = passval;
for (placeval = passval + 1; placeval < multitude-1; placeval++)
{
if (warriors[placeval].points < warriors[smallInd].points)
smallInd = placeval;
}
nouse = warriors[smallInd].points;
warriors[smallInd].points = warriors[passval].points;
warriors[passval].points= nouse;
}
}
/*This function will accept the array of struct datat type and number of
players(elements in array). From these
the function will take the value of points of each struct element of the array and
will add all of these to find
the total points.
input:array of struct data type from main
output:no output generated but the int value of total points will be returned
processing:simple for loop with combined assignment operator within the loop
*/
int total(info athlete[], int magnitude)
{
int overall=0;
for(int k=0; k<magnitude; k++)
{
overall+=athlete[k].points;
}
return overall;
}