为什么我的循环运行几次并且不要求输入?

时间:2018-01-03 09:15:11

标签: c++ visual-c++

我使用Visual Studio Community 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;
}

2 个答案:

答案 0 :(得分:2)

您的代码存在一些问题:

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

while (answer == 1)

湾在最后一个切换案例中,您应该在每个案例的末尾添加一个break命令。否则,它也会在默认选项下自动执行默认代码块。

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

        case 'n':
            answer = 1;
            break;

        default:
            answer = 2;
            break;
    }

℃。第一个开关盒块目前仅包括一个盒子。因此,它并不是真正需要的。

答案 1 :(得分:-1)

这种行为的原因是总会有一个&#39; n&#39;在键盘缓冲区。 Here解决了这个问题!

(除了已经提到过的无限循环......)

我引用了我已经链接的答案:

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

//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";
}

请参阅the C++ FAQ以及其他示例,包括在条件中添加最小值和/或最大值。

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