gpt4 book ai didi

c++ - do-while 中的输入验证

转载 作者:行者123 更新时间:2023-11-30 02:26:11 26 4
gpt4 key购买 nike

我遇到输入错误问题。我的输入已通过验证,但如果存在无效输入,我的错误消息“无效范围。请输入范围 0-100:”不会显示。

这是一个例子:

Please Enter Grade 1: 101 (It does not accept input)
Please Enter Grade 1: 321 (It does not accept input)

而不是显示:

Please Enter Grade 1: 101 (It does not accept input)

Invalid Rage. Please Enter Range 0-100.
Please Enter Grade 1:

这是我的代码:

for(vector<double>::size_type i = 0; i < 15; i++)
{
do
{
cout << "Please Enter Grade "<< i + 1 <<": " << flush;
cin >> gradesVector[i];
}
while(gradesVector[i] < 0.0 || gradesVector[i] > 100.0);
{
cout << "Invalid Rage. Please Enter Range 0-100:\n";
cout << "Please Enter Grade "<< i + 1 <<": " << flush;
cin >> gradesVector[i];
}
}

最佳答案

你的 while()条件应该是

while(gradesVector[i] < 0.0 || gradesVector[i] > 100.0)并使用此代码

for(vector<double>::size_type i = 0; i < 15; i++)
{
int c=0;
do
{
if(c!=0) //checking that we didnt entered loop for first time
cout << "Invalid Rage. Please Enter Range 0-100:\n";

cout << "Please Enter Grade "<< i + 1 <<": " << flush;
cin >> gradesVector[i];
c++;
}
while(gradesVector[i] < 0.0 || gradesVector[i] > 100.0);

}

关于c++ - do-while 中的输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com