-1

This program works properly with only 1 printf scanf pair. when i add the second pair and run the program, it lets me input the first variable. but skips over and the second and terminates the program. whats wrong?

#include <stdio.h>

//Version A-1.0

main
{
    //Variables
    int HQ;
    char additionalbuilding;

    //Prompt user to specify a HQ level
    printf("Specify a HQ level. (1-20): ");
    scanf("%d",&HQ);
    printf("Would you like another building? (y/n): ");
    scanf("%c",&additionalbuilding);
}

1 Answers1

0

This is because the previous \n from the integer input stays in buffer.Use a getchar before the scanf to get rid of it

printf("Specify a HQ level. (1-20): ");
scanf("%d",&HQ);
getchar();
printf("Would you like another building? (y/n): ");
scanf("%c",&additionalbuilding);
Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29