0

I'm pretty new to C and I have encountered a problem. In my code, I want to get the age, gpa, and letter grade of the user. However, when I run the code, it only gets the inputs for the age and gpa, ignoring the letter grade. It only asks for 2 inputs all in all instead of 3. But if I were to individually check for inputs instead of simultaneously doing so, it functions correctly. Why is this happening?

Check my code below:

int main(void) {

  int age;
  double gpa;
  char grade;

  // int
  printf("Enter your age: ");
  scanf("%d", &age);

  // double
  printf("Enter your gpa: ");
  scanf("%lf", &gpa);

  // char
  printf("Enter your letter grade: ");
  scanf("%c", &grade);

  // prints
  printf("You are %d years old", age);
  printf("\nYour gpa is %f", gpa);
  printf("\nYour grade is %c", grade);

  return 0;
}

OUTPUT:

Enter your age: 12
Enter your gpa: 4.0
Enter your letter grade: You are 12 years old
Your gpa is 4.000000
Your grade is
Lucky
  • 129
  • 3
  • 11

2 Answers2

-1
   int main(void) {

  int age;
  double gpa;
  char grade;

  // int
  printf("Enter your age: ");
  scanf("%d", &age);

  // double
  printf("Enter your gpa: ");
  scanf("%lf", &gpa);
Getchar();

  // char
  printf("Enter your letter grade: ");
  scanf("%c", &grade);

  // prints
  printf("You are %d years old", age);
  printf("\nYour gpa is %f", gpa);
  printf("\nYour grade is %c", grade);

  return 0;
    }

Your grade scanf statment is reading a newline character from previous statement. So , add a getchar() before grade statement which reads the newline character

vegan_meat
  • 878
  • 4
  • 10
  • I get it now. Thank you very much. One more thing, when do I use getchar() exactly? Is it always necessary to put it when I use multiple scanfs, or is it used in special situations? – Lucky Nov 09 '21 at 05:00
  • @Lucky The `scanf()` consumes inputs until where you specify, and remember that a newline character `\n`, a space `' '` are all characters. use `getchar()` when **you need to get rid of un needed characters.** – Uduru Nov 09 '21 at 05:03
  • 1
    I think space before `%c` is better option it can not consume more memory – Faheem azaz Bhanej Nov 09 '21 at 05:05
  • Getchar reads a single character from the standard input stream stdin.for example if you type 1, a 5 and then press the ENTER key. So there are now three characters in the input buffer. scanf("%d") reads the 1 and the 5. – vegan_meat Nov 09 '21 at 05:06
  • Thank you for your insights, guys! It's really helpful. I really appreciate it. I think I need to read more regarding this matter to cure my ignorance. – Lucky Nov 09 '21 at 05:10
-1

Put scanf(" %c", &grade) instead of scanf("%c", &grade) because you need a whitespace before the %c to skip the newline that was not read when scanf stopped at the end of the number

#include <stdio.h>

int main(void) {

  int age;
  double gpa;
  char grade;

  printf("Enter your age: ");
  scanf("%d", &age);

  printf("Enter your gpa: ");
  scanf("%lf", &gpa);

  printf("Enter your letter grade: ");
  scanf(" %c", &grade);

  printf("You are %d years old", age);
  printf("\nYour gpa is %f", gpa);
  printf("\nYour grade is %c", grade);

  return 0;
}
Faheem azaz Bhanej
  • 2,328
  • 3
  • 14
  • 29