Program doesn't prompt user for last input, but loops for second book - Data structures

后端 未结 3 1582
余生分开走
余生分开走 2021-01-24 11:31

I\'m learning about data structures. I\'ve written a simple program that asks the user to fill in information on a 3 book purchase. Like name of book, author, cost and pages.

3条回答
  •  离开以前
    2021-01-24 12:14

    Change like this

     for(ctr = 0; ctr < 3; ctr++)
        {
            printf("What is the name of the book #%d?\n", (ctr+1));
            gets(books[ctr].title);
            puts("Who is the author? ");
            gets(books[ctr].author);
            puts("How much did the book cost? ");
            scanf("%lf$", &books[ctr].price); //Changed
            puts("How many pages in the book? ");
            scanf(" %d", &books[ctr].pages);
            getchar();  // Clears last newline for the next loop
        }
    

    scanf man page

提交回复
热议问题