0
#include <stdio.h>

int main() {
int yos;
char gen;
char qual;

printf("Enter your sex (M/F):\n");
scanf("%c", &gen);
printf("Enter your year(s) of service:\n");
scanf("%d", &yos);
printf("Enter your qualification (G/P):\n");
scanf("%c", &qual);

return 0;
}

When I run the above code, I am able to enter values for the first two scanf, but I cannot enter a value for the last scanf. Why is that happening?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
truth313
  • 21
  • 5
  • Change `"%c"` to `" %c"` so it skips over the newline that ended the years of service. – Barmar Oct 10 '22 at 17:22
  • The first thing you need to do is check the return value from each `scanf` call, which is the number of fields it successfully extracted. In your case, you want it to be 1. I suspect your `scanf` calls are consuming newlines where you don't expect it, so you should change the formats by adding a space at the front, which will cause it to consume white space. – Tom Karzes Oct 10 '22 at 17:23
  • @TomKarzes It worked! Thank you so much! Enjoy: https://youtu.be/qnkuBUAwfe0 ! – truth313 Oct 10 '22 at 18:05
  • @Barmar It worked! Thank you so much! Enjoy: https://youtu.be/qnkuBUAwfe0 ! – truth313 Oct 10 '22 at 18:05

0 Answers0