输入的数字与代码显示的数字不同,在结构中使用数组,为什么?

Number that is inputted is not the same as displayed by the code, using array in a struct, why?

本文关键字:数字 数组 为什么 结构 代码 显示 输入      更新时间:2023-10-16

我正在制作一个代码,用户可以在其中键入数字,这些数字将存储在结构内部的数组中。但是,有时,我输入的值与存储/显示的值不同。这个问题不是很一致。例如,当我输入10时,它可以显示为:63846446382852或实际10。我对此感到非常困惑,我尝试使用 int、long 和 double 更改数组数据类型,但无济于事。

#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int n,x,totalAge = 0,maxAge = 0,avgAge = 0,maxGoals = 0,bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> n;
cout << "Input the number of games: ";
cin >> x;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
long goals[];
};
players goals[x];
players playerList[n];
for (int i = 0; i < n; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin.ignore();
getline (cin, playerList[i].name);
cout << "Input player's current team: ";
getline (cin, playerList[i].currTeam);
cout << "Input player's shirt number: ";
cin >> playerList[i].shirtn;
cout << "Input player's age: ";
cin >> playerList[i].age;
cout << "Input player's height (m): ";
cin >> playerList[i].height;
cout << "Input player's weight (kg): ";
cin >> playerList[i].weight;
cout << endl;
for (int a = 0; a < x; a++) {
playerList[i].goals[a] = 0;
playerList[i].totalGoals = 0;
}
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> playerList[i].goals[a];
playerList[i].totalGoals += playerList[i].goals[a];
}
if (playerList[i].totalGoals > maxGoals) {
maxGoals = playerList[i].totalGoals;
bestPlayer = i;
}
if (playerList[i].age > maxAge) {
maxAge = playerList[i].age;
oldest = i;
}
totalAge += playerList[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << playerList[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << playerList[i].currTeam << endl;
cout << "Shirt Number: " << playerList[i].shirtn << endl;
cout << "Age: " << playerList[i].age << endl;
cout << "Height: " << playerList[i].height << " m" << endl;
cout << "Weight: " << playerList[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << playerList[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / n;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << playerList[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << playerList[bestPlayer].name << ", shirt number: " << playerList[bestPlayer].shirtn << ". With total goals of: " << playerList[bestPlayer].totalGoals << endl;

}

您的代码的更正版本是这样的:

#include<string>
#include<iostream>
#include<vector>    //for vector
using namespace std;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
vector<long> goals;    //used vector instead of array
};
int main () {
int N, X;
int totalAge = 0, maxAge = 0, avgAge = 0, maxGoals = 0, bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> N;
cout << "Input the number of games: ";
cin >> X;
players player[X];
for (int i = 0; i < N; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin>>player[i].name;
cout << "Input player's current team: ";
cin>>player[i].currTeam;
cout << "Input player's shirt number: ";
cin >> player[i].shirtn;
cout << "Input player's age: ";
cin >> player[i].age;
cout << "Input player's height (m): ";
cin >> player[i].height;
cout << "Input player's weight (kg): ";
cin >> player[i].weight;
cout << endl;
player[i].totalGoals = 0;
for (int a = 0; a < X; a++) {
long G;
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> G;
player[i].goals.push_back(G);
player[i].totalGoals += G;
}
if (player[i].totalGoals > maxGoals) {
maxGoals = player[i].totalGoals;
bestPlayer = i;
}
if (player[i].age > maxAge) {
maxAge = player[i].age;
oldest = i;
}
totalAge += player[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < N; i++) {
cout << player[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << player[i].currTeam << endl;
cout << "Shirt Number: " << player[i].shirtn << endl;
cout << "Age: " << player[i].age << endl;
cout << "Height: " << player[i].height << " m" << endl;
cout << "Weight: " << player[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < X; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << player[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / N;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << player[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << player[bestPlayer].name << ", shirt number: " << player[bestPlayer].shirtn << ". With total goals of: " << player[bestPlayer].totalGoals << endl;
return 0;
}