Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用scanf在结构中存储字符串_C_Arrays_Function_Structure_Scanf - Fatal编程技术网

使用scanf在结构中存储字符串

使用scanf在结构中存储字符串,c,arrays,function,structure,scanf,C,Arrays,Function,Structure,Scanf,我已经编写了一个函数,将书籍信息存储到structbook中,除了书名有空格外,其他一切都正常工作。因此,像Dracula这样的东西会被很好地存储和显示,但像lordoftherings这样的东西只会跳过对本书其余信息的扫描,并继续关闭程序。是否有任何方法可以使用scanf将字符串注册为空格?这是我的密码: void addBook(struct Book book[], int *size) { if(*size== MAX_BOOKS) { p

我已经编写了一个函数,将书籍信息存储到structbook中,除了书名有空格外,其他一切都正常工作。因此,像
Dracula
这样的东西会被很好地存储和显示,但像
lordoftherings
这样的东西只会跳过对本书其余信息的扫描,并继续关闭程序。是否有任何方法可以使用scanf将字符串注册为空格?这是我的密码:

void addBook(struct Book book[], int *size)
{
        if(*size== MAX_BOOKS) {
                printf("the inventory is full.\n");
        }

        else {
                printf("ISBN\n");
                scanf("%d", &book[*size]._isbn);
                printf("Title\n");
                scanf("%s", book[*size]._title);
                printf("Price\n");
                scanf("%f", &book[*size]._price);
                printf("Year\n");
                scanf("%d", &book[*size]._year);
                printf("Qty\n");
                scanf("%d", &book[*size]._qty);
                printf("Book successfully added to inventory\n");

                (*size)++;
                }

}

如果需要,我可以使用完整的代码进行更新。

没问题。这是因为%s在第一个空格处停止。使用%[^\n]使其读取,直到他找到输入。

可能的重复项可以使用
fgets()
getline()
scanf(“%[^\n]s”,name)我试过
scanf(“%[^\n]s”,书[*size]。\u title)
会完全跳过输入。请尝试以下操作:
printf(“Title\n”);getchar();获取(书本[*大小]。\u标题)
所以只要用户不使用enter键进行换行,scanf就会读取每个字符?