Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:07:35+00:00 2026-06-07T10:07:35+00:00

I have the following struct typedef char String[256]; typedef struct { String name; int

  • 0

I have the following struct

typedef char String[256];

typedef struct
{
    String name;
    int year;
    float price;
} Book;

Array of Books

int main(int argc, const char * argv[])
{    
    Book books[5];

    for (int i=0; i<5; i++) {
        books[i] = inputBook();
    }

    return 0;
}

inputBook() function

Book inputBook()
{
    Book myBook;

    //Name
    puts("Enter Book Name:");
    gets(myBook.name);

    //Publishing Year
    puts("Enter Book Publishing Year:");
    scanf("%i", &myBook.year);

    //Price
    puts("Enter Book Price:");
    scanf("%f", &myBook.price);

    return myBook;
}

For some reason the first book input is going well but when trying to input the second book and the second call to inputBook() I can set a book name, it jumps straight to the year import.

What is the problem ?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:07:37+00:00Added an answer on June 7, 2026 at 10:07 am

    To correct, replace:

    gets(myBook.name);
    

    with:

    scanf("%255s", myBook.name); /* 255 as name is 256 chars. */
    

    as scanf() will skip any whitespace characters, but gets() will not. A newline character is considered a whitespace character and there will be a newline remaining in stdin after the price has been entered causing gets() to read the newline and effectively read nothing.

    Worth reading: warning:gets function is dangerous

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
I have the following struct: typedef struct{ int vin; char* make; char* model; int
I have the following structure typedef struct DeviceInfo { char[30] name; char[30] serial Number;
Suppose I have the following code: typedef struct { char **p; } STRUCT; int
I have defined the following struct in C: typedef struct point{ float x; float
I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have a question. I have the following struct : typedef struct{ int vin;
I have the following for my HashTable structure: typedef char *HashKey; typedef int HashValue;
I have following struct defination: typedef union U08_16_t { unsigned long s32;//message32 unsigned char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.