C:第二次提示输入时出现扫描和打印错误



这段代码的前半部分运行没有问题。我可以输入第二组 char float int char 输入,但不确定为什么我的代码在显示提示中显示零。

/*Prompt the user and accept the following 4 types of values from a single 
input line: char int char float */
char ch1, ch2;
int num1;
float num2;
printf("Enter char int char float: ");
scanf("%c %d %c %f",&ch1, &num1, &ch2, &num2)   ;
printf("You entered: '%c' %d '%c' %.3fn", ch1, num1, ch2, num2);

/*Prompt the user and accept the following types of values from a 
single input line: char float int char */
char ch3, ch4;
float num3;
int num4;
printf("Enter char float int char: ");
scanf("%c %f %d %c",&ch3, &num3, &num4, &ch4);
printf("You entered: '%c' %.3f %d '%c'n", ch3, num3, num4, ch4);

空间是问题所在。

笔触ENTER的第一个输入被视为第二个scan的输入。使用如下所示的空格:

scanf(" [太空] %c %f %d %c",&ch3, &num3, &num4, &ch4);

此处了解更多详情

您可以使用

getchar() . getchar()采取未知的char形式stdin。第一次prinf后使用 。

printf("You entered: '%c' %d '%c' %.3fn", ch1, num1, ch2, num2);
getchar();//add this

最新更新