C - Scanf 没有得到输入
C - Scanf not getting input
我是 C 的新手,遇到了一个问题。在我的代码中,我想获取用户的年龄、gpa 和字母等级。但是,当我 运行 代码时,它只获取年龄和 gpa 的输入,忽略了字母等级。它总共只要求 2 个输入而不是 3 个。但是如果我要单独检查输入而不是同时检查输入,它会正常运行。为什么会这样?
检查下面我的代码:
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;
}
输出:
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
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;
}
您的成绩 scanf 语句正在读取上一条语句中的换行符。所以,在成绩声明之前添加一个 getchar()
读取换行符
把 scanf(" %c", &grade)
而不是 scanf("%c", &grade)
因为你需要在 %c
之前有一个空格来跳过当 scanf
停止在末尾时没有读取的换行符数
#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;
}
我是 C 的新手,遇到了一个问题。在我的代码中,我想获取用户的年龄、gpa 和字母等级。但是,当我 运行 代码时,它只获取年龄和 gpa 的输入,忽略了字母等级。它总共只要求 2 个输入而不是 3 个。但是如果我要单独检查输入而不是同时检查输入,它会正常运行。为什么会这样?
检查下面我的代码:
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;
}
输出:
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
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;
}
您的成绩 scanf 语句正在读取上一条语句中的换行符。所以,在成绩声明之前添加一个 getchar()
读取换行符
把 scanf(" %c", &grade)
而不是 scanf("%c", &grade)
因为你需要在 %c
之前有一个空格来跳过当 scanf
停止在末尾时没有读取的换行符数
#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;
}