[输入字符串时,c中的函数跳过行

问题描述 投票:0回答:1

当尝试使用gets函数输入字符串时,它会跳过第一个输入并跳至第二个一个不扫描第一个字符串。

#include <stdlib.h>

int main()
{
    while(1){
        char s1[20], s2[20], s3[20], s4[20], s5[20];
        char str[20];
        int x;
        if(ch==2) {
            puts("enter the name of the book");
            gets(s1);
            puts("enter the name of the author");
            gets(s2);
            puts("enter the publishing date");
            gets(s3);
            puts("enter the publisher address");
            gets(s4);
            puts("enter the category of the book");
            gets(s5);
            printf("book inserted\n");
        }

}

    return 0;
}

c string gets
1个回答
1
投票

也许您一开始不应该使用gets():

“警告:gets函数非常危险,因为它没有提供防止字符串s溢出的保护。GNUC库仅出于兼容性目的包含了它。您应该始终使用fgets或getline。要提醒您这一点,请使用链接器(如果使用GNU ld),则每次使用get都会发出警告。“

https://www.gnu.org/software/libc/manual/html_node/Line-Input.html#index-gets

© www.soinside.com 2019 - 2024. All rights reserved.