我试图让用户使用带有数组和 cin 的 while 循环输入他们的姓名,但是在输入最后一个人的姓名后,程序崩溃而不是继续前进。有没有办法解决这个问题,还是我需要完全改变代码?我对 c++ 也很陌生,所以可以尽可能简单地给出任何答案吗?
#include <iostream>
#include <string>
using namespace std;
int main()
{
unsigned int numberofplayers;
number://loop back here if more than 4 players
cout << "Number of players: ";
cin >> numberofplayers;
if(numberofplayers > 4 || numberofplayers < 1){
cout << "Invalid number, please enter a number from 1 to 4." << endl;
goto number;
}
string name[numberofplayers];
cout << "Enter your name" << endl;
int a = 1;
while(a < numberofplayers + 1){
cout << "Player " << a << ": ";
cin >> name[a];
cout << "Hello, " << name[a] << "." << endl;
a++;
}
}