0

I don't understand why it's taking only first input

Code is :-

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

It is taking only first input then displaying everything without waiting for user input. You can see the output image in the link given below

Here you can see output :-

output

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Rajarshi
  • 23
  • 1
  • 5

1 Answers1

-1

Try this :

#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);
}
Kanji Viroja
  • 493
  • 1
  • 7
  • 17