我不明白 cin.ignore() 是如何工作的。当我 运行 这段代码时,我的程序崩溃了
I don't understand how cin.ignore() works. When I run this piece of code, my program breaks down
有一个类的数组。我想输入一定数量的玩家,然后使用 for
循环输入玩家的名字。问题是我不明白如何使用 cin.ignore()
.
来避免程序崩溃
void main() {
int numberOfPlayers;
cout << "Input amount of players:";
getline(cin, numberOfPlayers);
Player** arrOfPlayers = new Player*[numberOfPlayers];
string newName;
for (int i = 0; i < numberOfPlayers; i++) {
cout << "\nInput player " << i + 1 << " nickname: ";
getline(cin, newName);
arrOfPlayers[i]->setName(newName);
}
}
您还没有分配个人球员
这个
Player** arrOfPlayers = new Player*[numberOfPlayers];
只分配一组 指针 给玩家
你还需要创建那些玩家
for (int i = 0; i < numberOfPlayers; ++i)
arrOfPlayers[i] = new Player;
有一个类的数组。我想输入一定数量的玩家,然后使用 for
循环输入玩家的名字。问题是我不明白如何使用 cin.ignore()
.
void main() {
int numberOfPlayers;
cout << "Input amount of players:";
getline(cin, numberOfPlayers);
Player** arrOfPlayers = new Player*[numberOfPlayers];
string newName;
for (int i = 0; i < numberOfPlayers; i++) {
cout << "\nInput player " << i + 1 << " nickname: ";
getline(cin, newName);
arrOfPlayers[i]->setName(newName);
}
}
您还没有分配个人球员
这个
Player** arrOfPlayers = new Player*[numberOfPlayers];
只分配一组 指针 给玩家
你还需要创建那些玩家
for (int i = 0; i < numberOfPlayers; ++i)
arrOfPlayers[i] = new Player;