gpt4 book ai didi

c++ - vector C++ 的输入验证

转载 作者:行者123 更新时间:2023-11-30 05:15:19 25 4
gpt4 key购买 nike

我的 do-while 循环的输入验证有问题。我的 do-while 循环只验证所有其他输入

这是一个例子:

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

Invalid Rage. Please Enter Range 0-100.
Please Enter Grade 1: 101 (It accapts this input)

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

Invalid Rage. Please Enter Range 0-100.
Please Enter Grade 2: 101 (It accapts this input)

这是我的代码:

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];
}
}

最佳答案

试试这个:

int main() {
vector<double> gradesVector(15);
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);
}
}

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

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