2017-06-19 95 views
0

我不明白为什么它是只服用第一输入下面的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); 
} 

它是只取第一输入,则无需等待用户输入显示的一切。你可以看到下面

在这里你可以看到输出链路输出图像给出: -

output

+5

的问题是不正确的使用scanf函数的'() ',用你提供的格式字符串,'scanf()'不能读取换行符。另请参阅[远离scanf的初学者指南](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html)(免责声明:我的文档...) –

+1

有了这么大的结构我会在打印函数中传递指针。你正在做的是将整个结构复制到堆栈中。 – ttdado

+0

@FelixPalmen谢谢 –

回答

-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); 
} 
+4

正确,但一个*真正*好的答案应该解释为什么...也许你想添加一些解释,理想情况下还是关于*缓冲区溢出*,这仍然是可能的。 –

+0

https://stackoverflow.com/questions/18425307/scanf-function-doesnt-work –

+0

感谢它的工作!但需要说明 –

相关问题