1

The code attached with this post is part of a bigger program but I am experiencing problems while taking in input for my structured variable 'title' and 'author'. The program seems to loop over it and move on to the next line. Also, while using [^\n] before 's' in the string specifier, it does the same however I read it is used to take a string as input using scanf. Read a few posts but can't figure out the issue. Just starting with C, any help would be appreciated!

struct books{
char title[30];
char author[30];
char subject[20];
int quantity;
int book_id;
char *category;
int count;
float price;
};
struct books book;

book.book_id=id;
printf("\n\n\t\tBook Name:\n\t\t");
scanf(" %s",book.title);
printf("\n\n\t\tAuthor:\n\t\t");
scanf(" %[^\n]s",book.author);
printf("\n\n\t\tQuantity:\n\t\t");
scanf("%d",&book.quantity);
printf("\n\n\t\tPrice:\n\t\t");
scanf("%f",&book.price);
Syed Hasan
  • 67
  • 2
  • 10
  • It might be the space before %s – Luci Mar 18 '17 at 07:39
  • 1
    Does your format specifier really contain `[ ^ \ n ]` rather than `[^\n]`? You don't want the extra spaces there either. The `[^\n]` specifies `\n` specifically rather then any whitespace as a string delimiter. If you tried that, then that is the code you should have posted. Does it work when the input does not contain spaces or is that a problem too? – Clifford Mar 18 '17 at 07:43
  • 1
    [mcve], please! BTW, the `s` after `[^\n]` is wrong and should be removed. This won't have any effect though but is completely superfluous. – Spikatrix Mar 18 '17 at 07:55
  • It is [^\n] and without the spacing it is not working. The extra spaces was a mistake while copying, edited the post @clifford – Syed Hasan Mar 18 '17 at 07:59
  • @SyedHasan : If you "have read" about something; you should post a link or state what you actually read, because it was either wrong or you misunderstood it. the `[^\n` is not part of the format specifier, but a delimiter specification. – Clifford Mar 18 '17 at 08:03

1 Answers1

0

Use [^\n] before 's' in the string specifier in both the input "book.title" and "book.author".

I executed your code with those modifications. The code is correct.

You have been taking a space seperated String as the Title of Book.(like "Mein Kampf" as the Title.)

But you must understand that you cannot input space seperated words as a single String using Scanf. So the program loops over it and moves to the next line.

To take such inputs you will have to use gets or fgets function or use [^\n] before 's' in the string specifier.

  • gets(book.title) actually does the same. Loops over the title to the author. – Syed Hasan Mar 18 '17 at 08:04
  • You *can* accept strings with spaces using scanf. See the answer now marked as a duplicate. Whether it is wise to use scanf at all for string inpout is another matter. – Clifford Mar 18 '17 at 08:05
  • 1
    @SyedHasan : What do you mean by "loops over"? Your code fragment contains no loops. Do not use `gets()` - even worse that `scanf()`! The the question marked as duplicate, examples using fgets() and `scanf()` are given. – Clifford Mar 18 '17 at 08:07
  • Use [^\n] before 's' in the string specifier in both the input "book.title" and "book.author". I executed your code with those modifications. It runs smoothly. – Abhijeet Raj Mar 18 '17 at 08:13