我不明白为什么它是只服用第一输入下面的C代码是不工作
代码为: -
#include <stdio.h>
typedef struct _book
{
char title[100];
char author[50];
char genre[30];
}
books;
// function to get title from user
void get_title(books *b)
{
printf("Enter the title of the book: ");
scanf("%[^\n]", b->title);
}
// function to get author from user
void get_author(books *b)
{
printf("Enter name of the author: ");
scanf("%[^\n]", b->author);
}
// function to get genre from user
void get_genre(books *b)
{
printf("Enter the genre of the book: ");
scanf("%[^\n]", b->genre);
}
// function to display book title
void print_title(books b)
{
printf("Title of the book is: %s\n", b.title);
}
// function to display book author
void print_author(books b)
{
printf("Author of the book is: %s\n", b.author);
}
// function to display book genre
void print_genre(books b)
{
printf("Genre of the book is: %s\n", b.genre);
}
int main()
{
// defining book variable
books book;
// getting inputs from user
get_title(&book);
get_author(&book);
get_genre(&book);
// displaying outputs
printf("Details of the book :-\n");
print_title(book);
print_author(book);
print_genre(book);
}
它是只取第一输入,则无需等待用户输入显示的一切。你可以看到下面
在这里你可以看到输出链路输出图像给出: -
的问题是不正确的使用scanf函数的'() ',用你提供的格式字符串,'scanf()'不能读取换行符。另请参阅[远离scanf的初学者指南](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html)(免责声明:我的文档...) –
有了这么大的结构我会在打印函数中传递指针。你正在做的是将整个结构复制到堆栈中。 – ttdado
@FelixPalmen谢谢 –