473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fgets / buffer issue

3
Hi there

I'm facing some issue which I can't identify.
I have a structure 'album'. I try to read two albums from standard input. The first one works perfectly fine, but on the second one, it appears like this:
(..after first album has been read:)
title:
>band:
>
somehow, I can't enter a title, since 'band:' is already there..

I use this function to read an album:
Expand|Select|Wrap|Line Numbers
  1. void read_alb(struct album *alb) {
  2.   char buffer[MAX];
  3.  
  4.   printf("title:\n>");
  5.   fgets(buffer, MAX, stdin);
  6.   trim(buffer); /* removes newline */
  7.   strcpy((*alb).title, buffer);
  8.  
  9.   printf("band:\n>");
  10.   fgets(buffer, MAX, stdin);
  11.   trim(buffer);
  12.   strcpy((*alb).band, buffer);
  13.  
  14.   printf("year:\n>");
  15.   scanf("%d", &(*alb).year);
  16. }
This function is called twice. As already mentioned, it works the first, but not the second time.

I'd be grateful if somebody could give me some advice.

Regards,
rethab
Apr 4 '10 #1
2 2533
Banfa
9,065 Expert Mod 8TB
That is because on line 15 you scan a number. Since generally stdin works on lines, the input is not passed to your program until the user presses enter then the code on line 15 leaves data in the input buffer, possibly any characters that can not be converted to a integer (letters for example) plus definitely the newline at the end of the users input.

That newline (plus the possible characters) are then read by your next call to fgets and assigned as the next title. You can verify this by typing the year of the first album and the title of the second album on a single line when prompted for a year. The title will make its way into the album title of the second album.

The problem is you have mixed line mode (fgets) reads and formatted reads (scanf). You could fix it simply by used fgets to read the user input for year and atoi, strtol or sscanf to convert the input string to an integer.
Apr 4 '10 #2
rethab
3
Thank you for your detailed response, Banfa. I'll fix that.

Regards,
rethab
Apr 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: herrcho | last post by:
int get_lines(char *lines) { int n = 0; char buffer; puts("Enter one line at time; enter a blank when done"); while ((n < MAXLINES) && (gets(buffer) !=0 ) && (buffer != '\0')) { if ((lines...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
6
by: Eirik | last post by:
Hey, all groovy C programmers, I've read in the FAQ(question 12.18) about how applications skip calls to (f)gets() after scanf() has been used. How can I avoid this? I know that I can by putting...
20
by: Paul D. Boyle | last post by:
Hi all, There was a recent thread in this group which talked about the shortcomings of fgets(). I decided to try my hand at writing a replacement for fgets() using fgetc() and realloc() to read...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
7
by: RSoIsCaIrLiIoA | last post by:
until a poor newbie can build a better function than sscanf and fgets scanf("%s", string) is like gets(string)
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
9
by: Justme | last post by:
Novice programmer needs help with using fgets to read and ignore the first two lines of a file. I've gone thru the previous posting regarding fgets, but none of them seems to help my situation. I...
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
16
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to fgets() but also tells how many bytes it read...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.