为什么我的do循环运行了好几次而不请求输入? < >我使用Visual Studio社区2017来进行C++编程。我有以下代码。在这里,do-while循环运行了好几次,并且不会停止询问输入应该在哪里。但是,在最后一个switch case程序中,如果我输入1而不是n,该程序工作得很好。请帮忙 // Welcome2018.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream> #include<bitset> using namespace std; int main() { string month[] = { "January", "February", "March", "April", "May", "June", "July", "August" "September", "October", "November", "December" }; int m, d, answer; cout << "Welcome 2018!!!" << endl; do { cout << "Enter the number corresponding to the month you want displayed" << endl; cin >> m; switch (m) { case 1: cout << month[0] << endl; cout << "Enter the date to know the day it is/will be" << endl; cin >> d; 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; } } cout << "Are you sure you want to quit?" << endl; cout << "Enter Y/N based on your choice:"; cin >> answer; switch (answer) { case 1: answer = 1; case 'n': answer = 1; default: answer = 2; } } while (answer = 1); cout << "Thank You and Come Again!!!" << endl; return 0; }

为什么我的do循环运行了好几次而不请求输入? < >我使用Visual Studio社区2017来进行C++编程。我有以下代码。在这里,do-while循环运行了好几次,并且不会停止询问输入应该在哪里。但是,在最后一个switch case程序中,如果我输入1而不是n,该程序工作得很好。请帮忙 // Welcome2018.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream> #include<bitset> using namespace std; int main() { string month[] = { "January", "February", "March", "April", "May", "June", "July", "August" "September", "October", "November", "December" }; int m, d, answer; cout << "Welcome 2018!!!" << endl; do { cout << "Enter the number corresponding to the month you want displayed" << endl; cin >> m; switch (m) { case 1: cout << month[0] << endl; cout << "Enter the date to know the day it is/will be" << endl; cin >> d; 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; } } cout << "Are you sure you want to quit?" << endl; cout << "Enter Y/N based on your choice:"; cin >> answer; switch (answer) { case 1: answer = 1; case 'n': answer = 1; default: answer = 2; } } while (answer = 1); cout << "Thank You and Come Again!!!" << endl; return 0; },c++,visual-c++,C++,Visual C++,代码中的一些问题: a。在最后一个while语句中,应该使用检查相等性的“==”运算符,而不是执行赋值的“=”运算符 while (answer == 1) b。在最后一种开关情况下,应在每种情况的末尾添加一个break命令。否则,它也会在默认选项下自动执行默认代码块 switch (answer) { case 1: answer = 1; break; case 'n':

代码中的一些问题:

a。在最后一个while语句中,应该使用检查相等性的“==”运算符,而不是执行赋值的“=”运算符

while (answer == 1)
b。在最后一种开关情况下,应在每种情况的末尾添加一个break命令。否则,它也会在默认选项下自动执行默认代码块

    switch (answer)
    {
        case 1:
            answer = 1;
            break;

        case 'n':
            answer = 1;
            break;

        default:
            answer = 2;
            break;
    }

c。第一个开关盒块当前仅包括一个盒。因此,这并不是真正需要的

这种行为的原因是键盘缓冲区中总是有一个“n”这就是解决办法

除了前面提到的无限循环之外

我引用了我链接的答案:

程序进入无限循环的原因是由于输入失败而设置了std::cin的bad input标志。要做的事情是清除该标志并从输入缓冲区中丢弃错误输入

//executes loop if the input fails (e.g., no characters were read)
while (std::cout << "Enter a number" && !(std::cin >> num)) {
    std::cin.clear(); //clear bad input flag
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input
    std::cout << "Invalid input; please re-enter.\n";
}
请参阅,了解此示例和其他示例,包括在条件中添加最小值和/或最大值


另一种方法是将输入获取为字符串,并使用std::stoi或其他允许检查转换的方法将其转换为整数。

您有whileanswer=1,必须是whileanswer==1,您需要逐字显示输入、所需输出和实际输出。“工作正常”或“不工作”没有有效的状态报告。如果您将cin与int一起使用,则不能要求输入“n”!如果您确实为cin>>答案键入了n,则流将进入失败状态,并且在清除错误之前不会输入任何其他内容。虽然与您的问题没有直接关系,但我可以建议您阅读-这将为您解决所有问题?