[英]Date input only one integer not day,month and year in C
I want to write a C program that counts the years of a car by entering the Registration date. 我想编写一个C语言程序,通过输入注册日期来计算汽车的年数。 I can only use one int instead of day, month and year as integers. 我只能使用一个整数,而不是日,月和年作为整数。 How can I count with only the year if the whole date is the int? 如果整个日期都是int,如何只计算年份呢?
This is my code: 这是我的代码:
#include <stdio.h>
int main() {
int inputDateOfFirstUse, ageCalculation, age;
printf("\nProgram to calculate the age of a car");
printf("\nPlease enter the date of the first use (in this syntax tt.mm.jjjj): ");
scanf("%i", &inputDateOfFirstUse);
ageCalculation = inputDateOfFirstUse - 2018;
age = ageCalculation;
printf("\nThe car is %i years old.", age);
return 0;
}
In scanf
you can use the %*i
syntax to skip the values you don't care of. 在scanf
,可以使用%*i
语法跳过不需要的值。
#include <stdio.h>
int main() {
int inputDateOfFirstUse, ageCalculation, age;
printf("\nProgram to calculate the age of a car");
printf("\nPlease enter the date of the first use (in this syntax tt.mm.jjjj): ");
scanf("%*i.%*i.%i", &inputDateOfFirstUse); // Skips day and month, reads only year
ageCalculation = 2018 - inputDateOfFirstUse;
age = ageCalculation;
printf("\nThe car is %i years old.", age);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.