Visual Studio 2017中C++ do-while循环异常重复且不等待输入求助
问题分析与修复方案
我帮你拆解下代码里的问题,这些正是导致循环异常、输入不停止的根源:
核心问题点
1. do-while循环条件是赋值而非判断
你的循环条件写的是:
} while (answer = 1);
这里answer = 1是赋值操作,会把1强制赋给answer,并且这个表达式的结果永远是true,所以循环会无限执行,不管你输入什么都会一直重复。正确的写法应该是相等判断:
} while (answer == 1);
2. 输入类型不匹配导致输入流失效
answer被定义为int类型,但你让用户输入的是字符Y/N。当用户输入字符(比如'n')时,cin无法将字符转换为整数,会触发输入流的失败状态(failbit)。一旦输入流失效,后续所有的cin操作都会直接跳过,不会等待用户输入,这就是你看到“本该等待输入的位置不停止”的原因。
3. switch语句缺少break,逻辑完全混乱
第二个switch里的case没有加break,导致匹配任意case后都会执行后续所有代码:
switch (answer) { case 1: answer = 1; case 'n': answer = 1; default: answer = 2; }
比如输入1时,会先执行answer=1,然后因为没有break,继续走到default把answer改成2;输入'n'时,因为answer是int,'n'的ASCII值是110,所以会直接走到default设置answer=2,完全不符合你的预期。
4. 月份数组初始化错误
你的month数组中,"August"和"September"之间少了一个逗号,导致这两个字符串被拼接成一个,数组长度变成11而不是12,后续访问month[8]及以后会出现越界问题:
// 错误写法 string month[] = { "January", "February", "March", "April", "May", "June", "July", "August" "September", "October", "November", "December" }; // 正确写法 string month[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
5. 第一个switch仅处理了1月,其他月份无逻辑
当前代码只有case 1处理了1月的日期判断,输入其他月份数字时,switch里什么都不做,建议补充其他月份的逻辑,或者添加default提示输入无效。
修复后的完整代码
#include "stdafx.h" #include<iostream> #include<string> #include<limits> // 用于处理输入流清空 using namespace std; int main() { // 修复数组逗号问题 string month[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int m, d; // 把answer改成char类型,匹配用户输入的Y/N char answer; cout << "Welcome 2018!!!" << endl; do { cout << "Enter the number corresponding to the month you want displayed" << endl; // 处理输入错误:如果输入不是整数或超出1-12范围,重新输入 while (!(cin >> m) || m < 1 || m > 12) { cin.clear(); // 重置输入流状态 cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清空缓冲区的错误输入 cout << "Invalid input! Please enter a number between 1 and 12: "; } switch (m) { case 1: cout << month[0] << endl; cout << "Enter the date to know the day it is/will be" << endl; // 处理日期输入错误 while (!(cin >> d) || d < 1 || d > 31) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Invalid date! Please enter a number between 1 and 31: "; } if (d == 7 || d == 14 || d == 21 || d == 28) { cout << "The day on " << d << " January is Sunday!" << endl; } else if (d == 1 || d == 8 || d == 15 || d == 22 || d == 29) { cout << "The day on " << d << " January is Monday!" << endl; } else if (d == 2 || d == 9 || d == 16 || d == 23 || d == 30) { cout << "The day on " << d << " January is Tuesday!" << endl; } else if (d == 3 || d == 10 || d == 17 || d == 24 || d == 31) { cout << "The day on " << d << " January is Wednesday!" << endl; } else if (d == 4 || d == 11 || d == 18 || d == 25) { cout << "The day on " << d << " January is Thursday!" << endl; } else if (d == 5 || d == 12 || d == 19 || d == 26) { cout << "The day on " << d << " January is Friday!" << endl; } else if (d == 6 || d == 13 || d == 20 || d == 27) { cout << "The day on " << d << " January is Saturday!" << endl; } break; // 添加break,避免后续case串流 // 可以在这里补充其他月份的case逻辑 default: cout << month[m-1] << endl; cout << "Day lookup logic for this month is not implemented yet." << endl; break; } cout << "Are you sure you want to quit?" << endl; cout << "Enter Y/N based on your choice: "; // 清空缓冲区残留的换行符,避免直接读取空字符 cin.ignore(numeric_limits<streamsize>::max(), '\n'); cin >> answer; // 统一转换为小写,兼容大小写输入 answer = tolower(answer); // 调整逻辑:输入'n'继续循环,输入'y'退出 switch (answer) { case 'n': answer = 1; break; case 'y': answer = 2; break; default: cout << "Invalid input! Assuming you want to continue." << endl; answer = 1; break; } } while (answer == 1); // 修复为相等判断 cout << "Thank You and Come Again!!!" << endl; return 0; }
额外优化说明
- 把
answer改成char类型,完美匹配用户输入的字符; - 添加了输入错误处理:当用户输入非整数或超出范围的数字时,会提示并重新等待输入;
- 对输入的字符统一转小写,不管用户输入'N'还是'n'都能正确识别;
- 补充了第一个switch的
default分支,处理其他月份的情况; - 所有case都添加了
break,彻底避免逻辑串流。
现在运行修复后的代码,应该就能正常等待输入,循环也会按照你的预期执行了!
内容的提问来源于stack exchange,提问作者Anonymous
相关产品推荐
相关产品推荐

