[英]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.