0

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;
}
jpw
  • 44,361
  • 6
  • 66
  • 86
rayzor
  • 40
  • 1
  • 7

3 Answers3

3

The problem is that after the user entered their year and pressed enter the new line character was left in the input buffer so in the next pass it reads '\n' and exits the loop. Use scanf(" %c", &again); inside the while loop.

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;
}
StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
  • Thanks RatHat. That works perfect! So just for my understanding, was it the \n on the question in the loop that was causing the issue? Would you say it is better to put a space in scanf (like above) in the future? Cheers – rayzor Jun 20 '16 at 03:17
  • When the user entered the year say 1234 and pressed enter '\n' the value stored in the input buffer was 1234\n so next time you call scanf it's going to grab '\n' and exit the loop. Would it be better or not to use a space... depends on what you are doing, for example scanf("\n%c", &again); or flushing the input buffer would've work as well, just keep in mind that new line char when using scanf and trying to get chars. – StaticBeagle Jun 20 '16 at 03:31
  • Got it. Thanks for the explanation. – rayzor Jun 20 '16 at 03:35
  • Remember this happens when you are looking for a char with scanf, whenever you asked for the year scanf("%i", &year); it worked like a champ. – StaticBeagle Jun 20 '16 at 03:39
  • Excellent char only, noted! :-) – rayzor Jun 20 '16 at 13:37
  • %i skips all that nonsense and looks for a number ;), blanks, new lines and the works! but \'n' is a char so char has to deal with it. Glad I could help. – StaticBeagle Jun 20 '16 at 15:31
1

When you do printf() ending with \n, next scanf() will take it.

Try this

scanf(" %c", &again)
       ^ a space here

scanf("\n%c", &again)

Or remove \n from printf because you might not want cursor to go to next line

Or use getchar()

You can flush or remove 1 character from buffer before scanf()

Source Program doesn't wait for user input with scanf("%c",&yn);

Community
  • 1
  • 1
Priyesh Kumar
  • 2,837
  • 1
  • 15
  • 29
0

Looks like code is correct, can you remove line feed character "\n" from your printf statement like below and try?

printf("Would you like to check if a year is a leap year? y or n");

Because similar kind of issue I faced in JAVA Scanner class.

Dhanabalan
  • 572
  • 5
  • 19