如何在for循环中进行错误捕获
How to do error trapping in a for-loop
你好,我想知道有谁知道如何在"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;
}
}
//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 >> score; // Save the entered score
// Test to see if the score is valid
if (score >= 0 && score <= 10) {
// If the score is valid, add it into the array and update the total
Qscore[QuizN] = score;
QTotal = QTotal + score;
QuizN++;
} else {
cout << "Please enter a score between 0 and 10" << endl;
}
}
- 更新为使用QuizN作为数组索引,谢谢
归功于ssnobody
我发现解决问题的解决方案是将Quiz--
添加到else语句中
//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--;
Quiz--;
}
相关文章:
- "error: no matching function for call to"构造函数错误
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 设置 Visual Studio for MPI: 找不到标识符错误
- "no matching function for call to 'Vector::Vector'"错误
- 错误消息:使用"string* +="后"no match for 'operator+='"
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- 如何修复输出日志中的"EnableInput can only be specified on a Pawn for its Controller"错误
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- C++生成文件错误"implicit entry/start for main executable"
- 学习 c++,编译错误"no matching function for call"
- For 循环 C++ 中的值错误
- 为什么我的 for 循环给出错误:X 没有命名类型
- 类方法 - 数据结构中 For 循环的运行时错误
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 如何'Invalid conversion'和'no match for operator'更正这些 c++ 错误
- 为什么当我没有变量或其他任何命名的东西时"no match for 'operator==' "错误?
- 结构错误:"no matching function for call to "
- Mongdb C++ 驱动程序编译错误 for document{}.
- Matlab 编码器错误 "for loop index expressions.." 如何修改代码?
- 语法错误/ for循环