Chrissi Maurer
Chrissi Maurer

Reputation: 23

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. 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?

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;
}

Upvotes: 2

Views: 367

Answers (1)

Jo&#235;l Hecht
Jo&#235;l Hecht

Reputation: 1842

In scanf you can use the %*i syntax to skip the values you don't care of.

#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;
}

Upvotes: 3

Related Questions