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
1
  • You have to upvote answer if it's helpful Commented Feb 10, 2022 at 8:32

2 Answers 2

-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;
}
Sign up to request clarification or add additional context in comments.

Comments

-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

5 Comments

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 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.
I think space before %c is better option it can not consume more memory
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.