简体   繁体   English

我正在编写一个程序,它输出用户输入的最早日期,但出现了一些问题

[英]I am writing a program which outputs the earliest date the user enter but some problems occurs

I am writing this program which prompt the user to enter a any number of dates and output the earliest when the user enter 0/0/0.我正在编写这个程序,它提示用户输入任意数量的日期,并在用户输入 0/0/0 时输出最早的日期。 But my program outputs the result after I input the first 2 dates.但是我的程序在输入前 2 个日期后输出结果。 And when I enter more dates, it keeps comparing them but at the end, it gives a wrong result.当我输入更多日期时,它会不断比较它们,但最后却给出了错误的结果。 Please help me fix it.请帮我修复它。 Here is my code这是我的代码

/*
//Input
Enter a date (mm/dd/yy): 3/6/08
Enter a date (mm/dd/yy): 5/17/07
Enter a date (mm/dd/yy): 6/3/07
Enter a date (mm/dd/yy): 0/0/0
//output
5/17/07 is the earliest date*/


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

int main(int argc, char const *argv[])
{
int month, day, year;
int month2, day2, year2;
printf("Enter a date (mm/dd/yy): ");
scanf("%d/%d/%d", &month2, &day2, &year2);

while(month != 0 && day != 0 && year != 0)
{
    printf("Enter a date (mm/dd/yy): ");
    scanf("%d/%d/%d", &month, &day, &year);


    if(year == year2)
    {
        if(month == month2)
        {
            if(day > day2)
            {
                printf("%d/%d/%d is the earliest date\n", month2, day2, year2);
            }
            else
            {
                printf("%d/%d/%d is the earliest date\n",month, day, year);
            }
        }
        else if (month > month2)
        {
            printf("%d/%d/%d is the earliest date\n", month2, day2, year2);
        }
        else
        {
            printf("%d/%d/%d is the earliest date\n",month, day, year);
        }

    }
    else if(year > year2)
    {
        printf("%d/%d/%d is the earliest date\n", month2, day2, year2);
    }
    else
    {
        printf("%d/%d/%d is the earliest date\n",month, day, year);
    }


} 
return 0;
}

The first time year, moth, day are used, they lack initialization.第一次使用year, moth, day ,它们缺乏初始化。

// int month, day, year;
int month = -1;
int day = 0;
int year = 0;
// ...
while(month != 0 && day != 0 && year != 0)

Otherwise OP is close, yet needs to change the earliest date as various new dates occur.否则 OP 很接近,但需要随着各种新日期的出现而更改最早的日期。 @Barmar . @巴马尔 Print the earliest date once, after the loop.在循环之后打印最早的日期一次。

The loop exit condition test needs to occur right after data input.循环退出条件测试需要在数据输入后立即发生。

while (1) {
  printf("Enter a date (mm/dd/yy): ");
  scanf("%d/%d/%d", &month, &day, &year);
  printf("%d/%d/%d is the date entered\n", month, day, year);
  if (!(month != 0 && day != 0 && year != 0)) {
    break;
  }

  int earlier = 0;
  if (year < year2) {
    earlier = 1;
  } else if (year == year2) {
    if (month < month2) {
      earlier = 1;
    } else if (month == month2) {
      if (day < day2) {
        earlier = 1;
      }
    }
  }
  if (earlier) {
    year2 = year; 
    month2 = month; 
    day2 = day;
  }
}

printf("%d/%d/%d is the earliest date\n", month2, day2, year2);

Other improvements include:其他改进包括:

Reading a line of input and then parsing for a date.读取一行输入,然后解析日期。

Checking for scan errors.检查扫描错误。

Flush the output to insure the prompt occurs before input.刷新输出以确保在输入之前出现提示。

printf("Enter a date (mm/dd/yy): ");
fflush(stdout);

Using better names.. Rather than year2 , perhaps early_year ?使用更好的名字......而不是year2 ,也许early_year

A do loop rather than while one as the loop iterates at least once.一个do循环而不是while one 因为循环至少迭代一次。 Also then month, day, year can move to the inside of the loop.然后month, day, year可以移动到循环内部。

Range checking for valid dates ...范围检查有效日期...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在编写一个程序,要求用户以24小时格式输入时间并显示计划航班的最接近出发时间 - I am writing a program which ask user to enter time in 24 hour format and diplay closest departute time of schedule flight 我正在编写一个程序,在该程序中,用户输入金额并显示应收税款,以根据每个收入组计算应缴税额? - I am writing a programm in which user enter a amount and programm display tax due calculatig percntage of tax as per thier income group? 我正在编写一个小的 c 程序来进行线程同步 - I am writing a small c program which does thread synchronization 我试图用C编写一个程序,提示用户输入名称,然后输出其首字母缩写 - I am trying to write a program in C that prompts user for a name and then outputs its initials 在C中编写程序,提示用户输入两个日期,然后指示日历上的日期 - Write a program in C that prompts the user to enter two dates and then indicates which date comes earlier on the calender 在 C 中编写一个程序,提示用户输入两个日期,然后指出哪个日期在日历上较早 - Write a program in C that prompts the user to enter two dates and then indicates which date comes earlier on the calendar 我正在编写一个程序,它将删除数组中的重复数字。 使用指针通过函数传递数组 - I am writing a program which will delete the repeated numbers in array. Passed the array through a function using pointer 我正在用C编写一个程序,该程序通过BST搜索存储在txt文件中的数字,但是有我找不到的错误 - I am writing a program in C which searches the number stored in a txt file through a BST but there is mistake which I can't find 为什么我通过该程序获得大量输出? - Why am I getting outputs of huge numbers with this program? 如何显示用户输入的最早日期? - How to display earliest date from user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM