简体   繁体   English

scanf()在第二次进入字符串

[英]scanf() to get in the string on the second time

What is wrong with the scanf() to get in the string on the second time, I can't input my string on the second time. scanf()在第二次获取字符串时出了什么问题,我第二次无法输入字符串。 I am not sure with the error that occurs, I can't get this program function well 我不确定发生的错误,无法很好地执行此程序功能

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //variables decleration
    char staff_name1[31];
    char staff_name2[31];
    float sales1, sales2;

    //input
    printf("Enter staff name\t> ");
    scanf("%[^\n]s", staff_name1);

    printf("Enter sales amount\t> ");
    scanf("%f", &sales1);

    printf("\nEnter staff name \t> ");//ERROR,CAN'T INPUT MY STRING
    fflush(stdin);
    scanf("%[^\n]s", staff_name2);

    printf("\nEnter sales amount\t> ");
    scanf("%f", &sales2);

    printf("\n");

    //output
    printf("Staff Name\t\t\t\tSales Amount\n");
    printf("===================\t\t=============\n");
    printf("%-20s \t%12.2f\n", staff_name1, sales1);
    printf("%-20s \t%12.2f\n", staff_name2, sales2);


}

my output of this code is as below: 我的这段代码输出如下:

warning: this program uses gets(), which is unsafe.

Enter staff name   > kh s
Enter sales amount > 134.14

Enter staff name   > 
Enter sales amount > 141243.14

Staff Name              Sales Amount
===================     =============
kh s                          134.14
                           141243.14

I can't input the second staff name. 我无法输入第二名职员姓名。 Can anyone please help me solve this?? 谁能帮我解决这个问题吗?

fflush(stdin);

is undefined behaviour in standard C. To flush the newline character, you could simply use getchar() instead. 是标准C中未定义的行为 。要刷新换行符,您可以简单地使用getchar()代替。

printf("\nEnter staff name \t> ");
getchar();
scanf("%[^\n]s", staff_name2);

I would also use fgets() instead of scanf to read a line and trim the newline if necessary, which offers better control over invalid inputs being entered by user and against buffer overflows. 我还将使用fgets()而不是scanf来读取行并在必要时修剪换行符,这可以更好地控制用户输入的无效输入以及防止缓冲区溢出。

You have three problems. 你有三个问题。

  1. I see that you use %[^\\n]s . 我看到您使用%[^\\n]s It is wrong. 这是错误的。 The s isn't part of the %[ specifier. s不是%[说明符的一部分。 So use %[^\\n] instead of %[^\\n]s 因此,请使用%[^\\n]而不是%[^\\n]s
  2. After you enter the value for sales1 , you press Enter . 输入sales1的值sales1 ,按Enter键 This character stays in the stdin (standard input stream). 该字符保留在stdin (标准输入流)中。 And when the next character for %[^\\n] is \\n , it will fail. 并且当%[^\\n]的下一个字符为\\n ,它将失败。 Fix this problem by adding a space before %[^\\n] . 通过在%[^\\n]之前添加空格来解决此问题。
  3. Using fflush on stdin invokes Undefined Behavior as per the C11 standard, although the behavior is well defined in some implementations. stdin上使用fflush根据C11标准调用未定义行为,尽管该行为在某些实现中已得到很好的定义。 It is better to remove it so that your code will be more portable. 最好将其删除,以使您的代码更具可移植性。

Additional notes: 补充笔记:

  • You can limit the amount of characters to be scanned so that you can avoid buffer overflows . 您可以限制要扫描的字符数,以便避免缓冲区溢出
  • You can check the return value of scanf to make sure it is successful. 您可以检查scanf的返回值以确保它成功。 All the scanf in your program will return 1 on success. 程序中的所有scanf成功返回1。


Fixed Program : 固定程序

 #include <stdio.h> #include <stdlib.h> //Unused header int main() { char staff_name1[31]; char staff_name2[31]; float sales1, sales2; printf("Enter staff name\\t> "); if(scanf(" %30[^\\n]", staff_name1) != 1) { printf("Could not scan staff_name1"); return -1; //Exit main with a return value of -1 } printf("Enter sales amount\\t> "); if(scanf("%f", &sales1) != 1) { printf("Could not scan sales1"); return -1; //Exit main with a return value of -1 } printf("\\nEnter staff name \\t> "); //fflush(stdin); UB! if(scanf(" %30[^\\n]", staff_name2) != 1) { printf("Could not scan staff_name2"); return -1; //Exit main with a return value of -1 } printf("\\nEnter sales amount\\t> "); if(scanf("%f", &sales2) != 1) { printf("Could not scan sales2"); return -1; //Exit main with a return value of -1 } printf("\\n"); //output printf("Staff Name\\t\\t\\t\\tSales Amount\\n"); printf("===================\\t\\t=============\\n"); printf("%-20s \\t%12.2f\\n", staff_name1, sales1); printf("%-20s \\t%12.2f\\n", staff_name2, sales2); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM