我不明白为什么它只采取第一次输入
代码是: -
#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);
}
它只接受第一个输入然后显示所有内容而无需等待用户输入。您可以在下面给出的链接中看到输出图像
在这里你可以看到输出: -
答案 0 :(得分:-1)
试试这个:
#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);
}