我正在尝试制作一个制作阳历的大程序,但此刻,我只是尝试使用一个解析输入日期的函数。一些函数(sscanf和fgets)我不确定如何将它们与程序的其余部分一起使用。这是我的尝试:
int main(int arg, char *argv[]) {
/*
* Request a date from the user
* in format DD-MM-YYYY, DD.MM.YYYY or DD/MM/YYYY
*/
date d;
char input_date[20];
fgets(input_date, 20, stdin);
printf("Enter your date of birth in the form DD/MM/YYYY:");
//fgets(d, 100, stdin);
sscanf(input_date,"%d", &d.day, &d.month, &d.year);
if (leapYearFeb(d.year, d.month)) {
d.day = d.day + 1;
}
if (parse_formatted_date(input_date, &d))
printf("date: %s\ndd = %d, mm = %d, yy = %d\n", input_date, d.day, d.month, d.year);
return 1;
return 0;
}
这是date.h头文件,我在这里调用parse_formatted_date:
#ifndef DATE_H_
#define DATE_H_
// Define the structure date.
typedef struct {
int day;
int month;
int year;
} date;
// Parses a string 'formatted_date' representing a date
// in format DD-MM-YYYY, DD.MM.YYYY or DD/MM/YYYY
// into a structure date 'parsed_date'.
void parse_formatted_date(char * formatted_date, date * parsed_date) {
const int ret = sscanf(formatted_date, "%d-%d-%d",
&parsed_date->day,
&parsed_date->month,
&parsed_date->year);
//printf("Day: %d, Month: %d, Year: %d\n", d.day, d.month, d.year);
//return ret == 3;
}
#endif
目前,我得到的错误是:
main_Assignment4.c: In function ‘main’:
main_Assignment4.c:22: error: void value not ignored as it ought to be
不确定这些错误是什么意思或如何修复? 谢谢!
答案 0 :(得分:2)
修改后的答案
// Parses a string 'formatted_date' representing a date
// in format DD-MM-YYYY, DD.MM.YYYY or DD/MM/YYYY
// into a structure date 'parsed_date'.
void parse_formatted_date(char * formatted_date, date * parsed_date)
{
sprintf(formatted_date, "%d-%d-%d",
parsed_date->day,
parsed_date->month,
parsed_date->year);
}
int main(int arg, char *argv[]) {
/*
* Request a date from the user
* in format DD/MM/YYYY
*/
date d;
char input_date[20];
printf("Enter your date of birth in the form DD/MM/YYYY:");
fgets(input_date, 100, stdin);
sscanf(input_date,"%d/%d/%d", &d.day, &d.month, &d.year);
parse_formatted_date(input_date, &d);
printf("date: %s dd = %d, mm = %d, yy = %d\n", input_date, d.day, d.month, d.year);
return 0;
}