2011-04-25 21 views
0

我的程序位於下方。由於某些原因,當我輸入我的第一個球員姓名(這應該是佩雷斯,胡安的形式,佩雷茲是姓氏),它嚇壞了,並完成了該計劃,併爲所有人提供了大大小小的數字我的號碼,並不要求我的結構數組中的任何其他信息。有沒有人現在推理呢?結構中字符串對象的問題

/* 
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; 
} 
+2

您是否試過在調試器中逐步調試程序?或者添加有用的跟蹤輸出? – 2011-04-25 21:22:18

回答

0

考慮使用類似這樣的東西cin.getline(person.name,50);其中50是獲得的最大字符數量,可以是任何你想要的或變量。您也可以使用cin.ignore來消除您不想要的額外輸入。

Grab a good C++ book too

-1

你應該做的一件事是爲每個元素分配一個新的信息對象。你做的方式是不正確的,因爲enter()函數正在返回一個本地指針,在函數返回後失去它的值。

所以,試試這個:

info *enter() 
{ 
    info *person = new info; 
    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; 
} 

當然,你必須改變聲明:

info *player[10]; 

及其引用。

一切完成後,不要忘記釋放你分配的東西。

+1

這不是問題,但可能會給OP帶來更多混淆。當然,numplay應該被驗證爲<= 10 – sehe 2011-04-25 21:42:25

+0

-1:這是一個內存泄漏等待在這個OP級別發生。 Out參數會是一個更好的建議,雖然這與他不正確地使用'cin'沒有任何關係,儘管之後會出現問題。 – AJG85 2011-04-25 22:53:42

1

音符如何,如果你進入單的話,程序運行好

看到CIN >>(INT)將設置輸入流的失敗()位現在here

,看文檔對於istream :: ignore,你應該在你的路上