簡體   English   中英

以下C代碼不起作用

[英]Following C code is not working

我不明白為什么只接受第一次輸入

代碼是:-

#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);
}

它僅接受第一個輸入,然后顯示所有內容,而無需等待用戶輸入。 您可以在下面給出的鏈接中看到輸出圖像

在這里您可以看到輸出:-

輸出

嘗試這個 :

#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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM