gpt4 book ai didi

c++ - 如何在 for 循环中进行错误捕获

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

你好,我想知道有谁知道如何在“for”循环中捕获错误我有我的循环工作,并且有一种方法可以识别输入了无效数字,但无效数字仍然占据我数组中的一个位置

我想知道是否有办法让程序忽略或丢弃无效输入。

        //for loop to collect quiz scores
for (Quiz=0 ; Quiz < 4; Quiz++) //loop to input and add 4 quiz grades
{
cout << "Enter grade for Quiz " << QuizN++ << " "; //output to screen
cin >> Qscore[Quiz]; //array in action taking scores inputted and assigning it a position inside the array

if (Qscore[Quiz] >= 0 && Qscore[Quiz] <= 10)
QTotal = QTotal + Qscore[Quiz];
else
{
cout << "Please enter a score between 0 and 10" << endl;
QuizN--;
}

最佳答案

for(Quiz = 0; Quiz < 4;) //loop to input and add 4 quiz grades
{
cout << "Enter grade for Quiz " << Quiz+1 << " "; //output to screen
cin >> Qscore[Quiz]; //array in action taking scores inputted and assigning it a position inside the array

if ((Qscore[Quiz] >= 0) && (Qscore[Quiz] <= 10))
{
QTotal += Qscore[Quiz];
++Quiz;
}
else
{
cout << "Please enter a score between 0 and 10" << endl;
}
}

关于c++ - 如何在 for 循环中进行错误捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31258176/

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