I want this code to ask a user to put in a year and then tell them if it is a leap year. Then ask them if they would like to check another year. At the moment, it asks the first question okay and gives back the answer.
The program then prints out the next question but doesn't wait for input and exits the while loop. I have looked for a resolution on the other posts and thought I maybe had it but it keeps doing the same thing. Could anyone help with what was supposed to be a program to write up and pass some time which has now become a headache? I've written many more complex programs and this one has me frustrated. I hope I've been clear. Much appreciated!
//Check if the year is a leap year
#include <stdio.h>
#define YES 1
#define NO 0
void leapyear()
{
int year;
printf("Enter a year: \n");
scanf("%i", &year);
int x = year % 4;
int answer;
if (x == 0)
{
answer = YES;
}
else
{
answer = NO;
}
if (answer == YES)
{
printf("The year %i is a leap year.\n", year);
}
else
printf("The year %i is not a leap year.\n", year);
}
int main (void)
{
char again;
printf("Would you like to check if a year is a leap year? y or n\n");
scanf("%c", &again);
while(again == 'y')
{
leapyear();
printf("Would you like to check if a year is a leap year? y or n\n");
scanf("%c", &again);
}
return 0;
}