mardi 23 novembre 2021

Having issues with this simple program

The program is just simply supposed to calculate the users age by subtracting their dob from the current year. When I run the program it compiles successfully but I get a long number such as -215863352. The if and else conditions are added just to test them out, I was writing various programs using them to make sure I understand the syntax in c. I figure I'm missing something simple but can't figure it out.

#include <stdio.h>
int main()
{
    int year;
    int cyear;
    int age = cyear - year;

    printf("Please enter the year you were born: \n");
    scanf("%i", &year);
    printf("Now enter the current year: \n");
    scanf("%i", &cyear);

    if (1+1 == 2){
        printf("You must be %i", age);
    }
    else {
        printf("Cannot compute age, GOODBYE:\n");
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire