#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
string hometown;
int age;
int games;
int* hours = new int [games];
};
int main(){
int players;
Student* s = new Student [players];
cout << "How many esports players are there at TTU who major in csc?\n";
cin >> players;
cout << "\nPlease enter in information about each player: \n\n";
for(int i = 0; i < players; i ){
cout << "PLAYER " << (i 1) << ":\n";
cout << "\tNAME:\t";
getline(cin >> ws, s[i].name);
cout << "\tHOMETOWN:\t";
getline(cin >> ws, s[i].hometown);
cout << "\tAGE:\t";
cin >> s[i].age;
cout << "\tHOW MANY GAMES DOES " << s[i].name << " PLAY?\t";
cin >> s[i].games;
for(int j = 0; j < s[i].games; j ){
cout << "NUMBER OF HOURS " << s[i].name << "PLAYED GAME" << (j 1) << "\t";
cin >> s[i].hours[j];
}
}
return 0;
}
出于某種原因,for 回圈在詢問年齡后結束。我試過多個輸入,它做同樣的事情。我是 C 的新手。有誰知道為什么?感謝您的時間
uj5u.com熱心網友回復:
您沒有正確處理陣列。您正在使用未初始化的變數為其大小分配它們:
- in
int games; int* hours = new int [games];,games未初始化。 - in
int players; Student* s = new Student [players];,players未初始化。
而且,即使您正確分配了陣列,您也會泄漏它們,因為在使用完它們后您并沒有釋放它們。
當您需要使用直到運行時才知道其大小的陣列時,您可以正確使用new[],但在您確定大小之后才能使用,例如:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
string hometown;
int age = 0;
int games = 0;
int* hours = nullptr;
};
int main(){
cout << "How many esports players are there at TTU who major in csc?\n";
int players;
cin >> players;
Student *s = new Student[players];
cout << "\nPlease enter in information about each player: \n\n";
for(int i = 0; i < players; i){
cout << "PLAYER " << (i 1) << ":\n";
cout << "\tNAME:\t";
getline(cin >> ws, s[i].name);
cout << "\tHOMETOWN:\t";
getline(cin >> ws, s[i].hometown);
cout << "\tAGE:\t";
cin >> s[i].age;
cout << "\tHOW MANY GAMES DOES " << s[i].name << " PLAY?\t";
cin >> s[i].games;
s[i].hours = new int[s[i].games];
for(int j = 0; j < s[i].games; j){
cout << "NUMBER OF HOURS " << s[i].name << " PLAYED GAME " << (j 1) << "\t";
cin >> s[i].hours[j];
}
}
// use player info as neded...
for(int i = 0; i < players; i){
delete[] s[i].hours;
}
delete[] s;
return 0;
}
話雖如此,您應該手動使用/std::vector代替,例如:new[]delete[]
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
string hometown;
int age = 0;
int games = 0;
std::vector<int> hours;
};
int main(){
cout << "How many esports players are there at TTU who major in csc?\n";
int players;
cin >> players;
std::vector<Student> s(players);
cout << "\nPlease enter in information about each player: \n\n";
for(int i = 0; i < players; i){
cout << "PLAYER " << (i 1) << ":\n";
cout << "\tNAME:\t";
getline(cin >> ws, s[i].name);
cout << "\tHOMETOWN:\t";
getline(cin >> ws, s[i].hometown);
cout << "\tAGE:\t";
cin >> s[i].age;
cout << "\tHOW MANY GAMES DOES " << s[i].name << " PLAY?\t";
cin >> s[i].games;
s[i].hours.resize(s[i].games);
for(int j = 0; j < s[i].games; j){
cout << "NUMBER OF HOURS " << s[i].name << " PLAYED GAME " << (j 1) << "\t";
cin >> s[i].hours[j];
}
}
// use player info as needed...
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374260.html
上一篇:在for回圈中增加x的值
