输入错误后循环返回,fgetc(stdin) 问题和循环

Loop back after wrong input, problem with fgetc(stdin) and loop

所以我找到了这个简单的代码 here。我想让它循环工作。我用不同的方法尝试了几次,但输出很糟糕。在 Ubuntu Visual Studio

工作

编辑 我添加了 if(y>=2015 && y<=3000) 并且它工作正常?

编辑 2 我已经修改了我的代码并遵循了@Sergey 的建议......它仍然无法正常工作...... 我尝试添加 "Check return value of scanf" 但它也不起作用。

        if ((scanf("%u", &d) == 1) && (scanf("%u", &m) == 1) && (scanf("%u", &y) == 1))
            while (fdateCheck());
        else
    //...EOF or conversion failure...
            while (fdateCheck());

while ((rc = scanf("%u.%u.%u", &d, &m, &y)) != EOF)
{
    if (rc != 3)
        //...oops data problems...
    else
        //...all OK...
}

需要有关检查 return scanfs

的建议
int fdateCheck();

unsigned int d,m,y;
unsigned int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int rc;
int legit = 0;

int main()
    {
        printf("Enter the date\n");
        scanf("%u.%u.%u",&d,&m,&y);
        while (fdateCheck());
    }
int fdateCheck()
    {   
        if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
            {
                daysinmonth[1]=29;
            }
            else if (y >= 2015 && y <= 3000)
                {
                    if (m < 13)
                        {
                            if (d <= daysinmonth[m-1])
                            legit = 1;
                        }

                    if (legit == 1)
                        {
                            system("clear");  
                            printf("It is a legitimate date!\n");
                            return 0;
                        }
                }
            else
                system("clear");     
                int ch = fgetc(stdin);
                if (ch == '\n')
                    {
                        system("clear");
                        printf("It's not a legitimate date!\n");
                        printf("\nRetry: ");
                        return 1;
                    }
                fflush(stdin);
    }

如果您在函数 void main() 中有适当的代码并且需要按照您的描述在一个循环中重复它,您可以执行以下操作:

  1. 将原来的 void main() 重命名为 int one_step()
  2. return 0;放在printf("It is a legitimate date!\n");
  3. 之后
  4. return 1;放在printf("It's not a legitimate date!\n");
  5. 之后
  6. 创建新的 void main() 函数为:
void main() {
    while(one_step());
}

您不需要 break;return main();。您可能需要一些增强功能,如上面的评论所述。祝编码顺利!

更新 #1

我的意思是对您的代码进行这样的改进:

#include <stdio.h>
#include <stdlib.h>

int fdateCheck();

int main()
{
    while (fdateCheck());
}

int fdateCheck()
{
int res;
int legit = 0;
unsigned int d,m,y;
unsigned int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    // get input block
    printf("Enter the date\n");         // prompt for data
    res = scanf("%u.%u.%u",&d,&m,&y);   // get input data
    if (res == EOF) {                   // check if no more input
            printf("Input error\n");
            exit(1);
    }
    // check if input data is valid
    if (res != 3) {                     // check if 3 numbers scanned
            system("clear");
            printf("It is not a date\n");
            fflush(stdin);
            return 1;
    }
    // make leap year correction
    if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
        daysinmonth[1]=29;              // leap year correction
    }
    // check if year, month and day is valid
    if (y >= 2015 && y <= 3000) {       // check if year in this range
        if (0 < m && m < 13) {          // check if month in this range
            if (0 < d && d <= daysinmonth[m-1]) // check if day in this range
                legit = 1;
        }
    }
    // print a message and finish the iteration
    if (legit == 1) {
        printf("It is a legitimate date!\n");
        return 0;
    }
    else {
        system("clear");
        printf("It's not a legitimate date!\n");
        printf("Please retry.\n");
        fflush(stdin);
        return 1;
    }
}

我还移动了函数内的所有变量。这将在每次迭代之前恢复 legitdaysinmonth[1] 的值。

更新#2

我的下一个提议:

#include <stdio.h>
#include <stdlib.h>

int fdateCheck();

int main()
{
    while (fdateCheck());
}

int fdateCheck()
{
int res;
int legit = 0;
unsigned int d,m,y;
unsigned int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    // get input block
    printf("Enter the date\n");         // prompt for data
    res = scanf("%u.%u.%u",&d,&m,&y);   // get input data
    if (res == EOF) {                   // check if no more input
            printf("Input error\n");
            exit(1);
    }
    // check if input data is valid
    if (res != 3) {                     // check if 3 numbers scanned
            fgetc(stdin);               // remove a barrier
            system("clear");
            printf("It is not a date\n");
            fflush(stdin);
            return 1;
    }
    // make leap year correction
    if (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) {
        daysinmonth[1]=29;              // leap year correction
    }
    // check if year, month and day is valid
    if (y >= 2015 && y <= 3000) {       // check if year in this range
        if (0 < m && m < 13) {          // check if month in this range
            if (0 < d && d <= daysinmonth[m-1]) // check if day in this range
                legit = 1;
        }
    }
    // print a message and finish the iteration
    if (legit == 1) {
        printf("It is a legitimate date!\n");
        return 0;
    }
    else {
        system("clear");
        printf("It's not a legitimate date!\n");
        printf("Please retry.\n");
        fflush(stdin);
        return 1;
    }
}