Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 永远的循环。。。未正确清除用户输入?_C++_Debugging - Fatal编程技术网

C++ 永远的循环。。。未正确清除用户输入?

C++ 永远的循环。。。未正确清除用户输入?,c++,debugging,C++,Debugging,当用户为“是否希望进入下一年?(y/N):”输入“y”或“y”时,将永远循环“该月有0天”。为什么? 我试着查看这些值,似乎存储的值正在被再次使用。可能我没有正确使用cin.clear() //variables bool ucontinue = true; //answer to continue int year = 0; int month = 0; int days = 0; char answer = 'a'; //loop while (ucontinue == true) {

当用户为“是否希望进入下一年?(y/N):”输入“y”或“y”时,将永远循环“该月有0天”。为什么? 我试着查看这些值,似乎存储的值正在被再次使用。可能我没有正确使用cin.clear()

//variables
bool ucontinue = true; //answer to continue
int year = 0;
int month = 0;
int days = 0;
char answer = 'a';

//loop
while (ucontinue == true)
{
    /*
    Enter a year (Must be a positive integer): 2016
    Enter a month (Must be a between 1 and 12): 2
    The month has 29 days.
    Do you wish to enter another year? (Y/N): y
    */


    //year input
    while (year <= 0)
    {
        cout << "Enter a year (Must be a positive integer): ";
        cin >> year;
    }

    //month input
    while (month <= 0)
    {
        cout << "Enter a month (Must be a between 1 and 12):";
        cin >> month;
    }

    //# of days in the month 
    cout << "The month has " << days << " days." << endl << endl;

    //continue?
    while (answer != toupper('y') && answer != toupper('n'))
    {
        cout << "Do you wish to enter another year? (Y/N): ";
        cin >> answer;
        answer = toupper(answer);

        if (answer == toupper('n'))
        {
            ucontinue = false;
        }

    }
        cin.clear();

}
//变量
bool ucontinue=真//继续回答
整年=0;
整月=0;
整数天=0;
char-answer='a';
//环路
while(ucontinue==true)
{
/*
输入年份(必须为正整数):2016年
输入月份(必须是介于1和12之间的数字):2
这个月有29天。
您是否希望进入下一年?(是/否):是
*/
//年度投入
而(年),;
}
//月输入
而(月),;
}
//#一个月中的天数

如果我理解正确,您可以执行以下操作:

Enter a year (Must be a positive integer): 2017
Enter a month (Must be a between 1 and 12):5
The month has 0 days.

Do you wish to enter another year? (Y/N): y <ENTER>
cin.clear();
此时,
year
month
仍处于设置状态。因此,在新循环迭代中,行

while (year <= 0)
已选中。由于我刚刚输入了
y
此条件不正确且

cin.clear();

然后执行,在此之后循环无限期重新启动。

您的程序有许多问题

  • 您从未在days变量上更新或输入过某些内容,因此在程序开始时将始终为0

  • if(answer==toupper('n'))
    可以简化为
    if(answer='n')


  • 因为您的while循环,所以您可以永远编写循环代码。第一次运行程序时,循环工作正常,但第二次循环时,所有值都已设置好,例如,第二次执行while语句时

    while (year <= 0)
    {
        cout << "Enter a year (Must be a positive integer): ";
        cin >> year;
    }
    
    while(年;
    }
    
    不会运行,因为year已经大于0,并且代码中的所有while语句都会发生这种情况。如果您使用do while语句而不是while语句,则会起作用,因为do while语句将在测试条件之前在循环中运行一次。如下所示:

    do
    {
        cout << "Do you wish to enter another year? (Y/N): ";
        cin >> answer;
        answer = toupper(answer);
    
    
        if (answer == toupper('n'))
        {
            ucontinue = false;
        }
    
    }while(answer != 'Y' && answer != 'N');
    
    do
    {
    回答;
    答案=toupper(答案);
    如果(答案==toupper('n'))
    {
    u继续=假;
    }
    }while(answer!=“Y”&&answer!=“N”);
    
    您的代码不会更改
    值。您需要根据
    的输入来更改它。您可能还需要一个函数来检查“闰年”。是的,我有这些天的伪代码,但我只是尝试确保其他while循环正常工作。谢谢!课程还没有结束r做一段时间,所以我避免把它放在我的代码中将清除所有用户输入——有没有原因不这样做?因此:将除
    u继续
    之外的所有变量移到外部循环中。这解决了循环问题,只剩下要添加的天数计算。正如其他地方所建议的那样:常量的上限没有用处-改为测试大写常量。此外,月份可以是0或更多答12.他或她需要做while语句而不是while语句这也会解决问题。对吗?是的,同意你答案中的
    do while
    比问题中的
    while
    好。现在我理解你的困惑:
    cin.clear()
    清除命令窗口中的输入,而不是分配给变量的值。它的功能没有您想象的那么强大。但这两个点都与循环问题无关。谢谢。我刚刚决定将所有变量(ucontinue除外)移动到外部while循环中。我还试图远离do-while循环,因为类还没有过do while。我想知道为什么cin.ignore()没有清除用户输入?我想你误解了cin.ignore()如果“清除用户输入”,您的意思是应该清除
    年、月、答案中的值吗?这是不可能的,但您可以在第一个循环开始时设置
    年=0,月=0,答案='a'
    ,这也应该解决问题
    cin.clear();
    
    while (year <= 0)
    {
        cout << "Enter a year (Must be a positive integer): ";
        cin >> year;
    }
    
    do
    {
        cout << "Do you wish to enter another year? (Y/N): ";
        cin >> answer;
        answer = toupper(answer);
    
    
        if (answer == toupper('n'))
        {
            ucontinue = false;
        }
    
    }while(answer != 'Y' && answer != 'N');