0

Why my code gets returned after scanning 1st char?

Code:

#include <stdio.h>
#include <stdlib.h>

int main() {
char kind; char neutered;

printf ("If Dog enter D, If Cat enter C, If Bird enter B, If reptile enter R\n");
printf("\nEnter the kind of Animal: ");
scanf ("%c" , &kind);
printf ("\nIs the Animal neutered? If YES enter 'Y' If NO enter 'N': ");
scanf ("%c" , &neutered);

if (kind == 'D' && neutered == 'Y')
printf ("\nInsurance fee for neutered dog is $40");

else if (kind == 'D' && neutered == 'N')
printf ("\nInsurance fee for non neutered dog is $50");

else if (kind == 'C' && neutered == 'Y')
printf ("\nInsurance fee for neutered cat is $450");

else if (kind == 'C' && neutered == 'N')
printf ("\nInsurance fee for non neutered cat is $65");

else if (kind == 'B' || kind == 'R')
printf ("\nInsurance fee for a bird or reptile is $0");

return 0;}

Output: If Dog enter D, If Cat enter C, If Bird enter B, If reptile enter R

Enter the kind of Animal: D

Is the Animal neutered? If YES enter 'Y' If NO enter 'N':

Process returned 0 (0x0) execution time: 7.031 Press any key to continue.

mu9u1t
  • 55
  • 1
  • 6

1 Answers1

1

Because scanf doesn't consume the newline, so you can use getchar() after the first scanf to swallow it.

scanf ("%c" , &kind);
getchar();
printf ("\nIs the Animal neutered? If YES enter 'Y' If NO enter 'N':");
scanf ("%c" , &neutered);
Hitokiri
  • 3,607
  • 1
  • 9
  • 29