Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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++_C++11_Visual C++_C++ Cli_Do While - Fatal编程技术网

C++ 日期验证。即使我的约会确实有效,也会继续循环

C++ 日期验证。即使我的约会确实有效,也会继续循环,c++,c++11,visual-c++,c++-cli,do-while,C++,C++11,Visual C++,C++ Cli,Do While,我尝试输入一个无效的日期,即2007年4月31日(4月是日历上的第4个月,只有30天),我的代码做了它应该做的事情,但是为什么我的do while循环即使输入了有效的日期也会继续循环 class test { int nmonths; int month; int day; int year; public: test() { int x; do { cout <&l

我尝试输入一个无效的日期,即2007年4月31日(4月是日历上的第4个月,只有30天),我的代码做了它应该做的事情,但是为什么我的do while循环即使输入了有效的日期也会继续循环

class test
{
    int nmonths;
    int month;
    int day;
    int year;

public: 
    test()
    {
        int x;
        do
        {
            cout << "Enter the date of most recent test (mm dd yyyy) (Ex. 01 
09 2011):" <<endl;
            cin >> month >> day >> year;
            int days;
            if ( month < 1 || month > 12 )
            {
                cout << "INVALID DATE!!!" << endl;

            }
            if ( month == 4 || month== 6 || month == 9 || month == 11)
            {
                days = 30;
                if( day < 1 || day > 30)
                {
                    cout << "INVALID DATE!!!" << endl;
                    x = 0;
                }
            }
            if ( month == 2)
            {
                bool leapyear = (year% 4 == 0 && year % 100 != 0) || (year % 
400 == 0);
                if (leapyear == 0)
                {
                    days = 28;
                    if (day < 1 || day > 28)
                    {
                        cout << "INVALID DATE!!!" << endl;
                        x = 0;
                    }       
                }
                else 
                    days = 29;
                    if ( day < 1 || day > 29 )
                    {
                        cout << "INVALID DATE!!!" << endl;
                        x = 0;
                    }
            }
            else 
            {
                days = 31;
                if ( day < 1 || day > 31)
                    {
                        cout << "INVALID DATE!!!" << endl;
                        x = 0;  
                    }
            }

        }while(x == 0);
};
类测试
{
整数毫微秒;
整月;
国际日;
国际年;
公众:
测试()
{
int x;
做
{
日期>>年;
国际日;
如果(月<1 | |月>12)
{

不能您从未将
x
设置为非零值。
尝试将
x=1;
添加到
cin

退出循环的条件是什么?如何将
x
变为
!=0
?魔法?