对于此循环,我需要能够在必须有100个元素长的数组中输入名称,并在输入Q或q或到达数组末尾时退出该数组以读取名称。当我使用此代码时,程序将返回到while循环的开头,而不会中断for循环。
for (int i = 0; i < 100; i++)
{
while (true)
{
cout << "Enter Player Name (Q to quit): ";
getline(cin,playerName[i]);
if (playerName[i] == "Q" || playerName[i] == "q")
break;
cout << "Enter score for " << playerName[i] << ": "<< endl << endl;
}
}
根据您的描述,似乎while (true)
完全是多余的!!!
因此,您应该简单地执行以下操作:
int i;
for (i = 0; i < 100; i++)
{
cout << "Enter Player Name (Q to quit): ";
getline(cin,playerName[i]);
if (playerName[i] == "Q" || playerName[i] == "q")
break;
cout << "Enter score for " << playerName[i] << ": "<< endl << endl;
}
此时,您可以使用i
来告诉用户输入了多少个名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句