【发布时间】:2019-10-03 02:30:48
【问题描述】:
使用包含的库,如何将包含姓名、年龄和比率的字符串与存储在数组中的 fget 的输入分开?
我需要修改字符串、年龄和格式以显示 3 位小数。之后,我需要将这些变量的新结果存储在单个 char 数组中。这就是我到目前为止所拥有的。我不知道是什么导致 sscanf() 给我意外的结果或如何解决它
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define SIZE 10
#define SIZE2 40
int main(){
char input[SIZE2]={};
char name[SIZE]={};
int age = 0;
float rate = 0;
printf("Enter name, age and rate (exit to quit): ");
fgets(input, SIZE2, stdin);
sscanf(input,"%s %d %f", name, &age, &rate);
printf("%s %d %.3f",name, &age, &rate);
return 0;
}
名字显示正常,但是年龄是一个随机的大数,比率显示为0.000
【问题讨论】: