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

标签 c loops date fgetc

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

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

EDIT 2 我已经修改了我的代码并遵循了@Sergey 的建议......它仍然无法正常工作...... 我尝试添加“检查 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...
}

需要有关检查 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. printf("It is a legal date!\n");
  3. 之后放 return 0;
  4. printf("It's not a legal date!\n"); 之后放置 return 1;
  5. 创建新的 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;
    }
}

关于c - 输入错误后循环返回,fgetc(stdin) 问题和循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57491653/

相关文章:

javascript - 有人可以向我解释一下 for-in 循环在这里是如何被调用 3 次的吗?

javascript - While 循环和innerHTML - 为什么它会反向显示数字?

mysql - 检查Mysql查询中3个不同日期范围内的值

c - C语言的ATM机

c - *s++ 中发生了什么?

c - C中判断一个字符串是否包含子字符串

java - 编译后的程序在第一个 "for"循环处停止

javascript - 保持午夜过后的营业时间

java - 使用 java.sql.Date 从当前日期添加 1 个月

c - 可变内存位置与符号 vs pmap