我不明白爲什麼它是隻服用第一輸入下面的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謝謝 –