Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with using gets() and scanf

Status
Not open for further replies.

darr01

Programmer
Oct 24, 2001
28
MY
Hi, all. I know this problem has been addressed in this forum but I am still facing some difficulties with my input.
Ok, I know that I should not mix scanf with gets but what I need to do is to ask my users to input a number of things into a file:
1) a string (e.g. a book title - I can't use scanf as it will only recognize the first word).
2) another string (e.g. publisher - same issue as above).
3) an interger (e.g. price of book)
4) a character (e.g. F - Fiction / N - Non-fiction.

I know the above strings can be accomplished using gets() but if I use scanf for my price, the input will be skipped.

There must be a way out of this problem but with what limited knowledge I have, I am stump!

Any help would be appreciated.

Thanks,
Darryl
 
#include <stdio.h>
#include <stdlib.h>

enum {

MAX_TITLE = 50,
MAX_PUB = 50,
MAX_PRICE = 10,
MAX_CLASS = 2

};


void Remove_NewLine( char *sBuffer,int iIndex );


void main( void ) {


char sBookTitle[MAX_TITLE];
char sBookPub[MAX_TITLE];
char sBookPrice[MAX_PRICE];
char sBookClass[MAX_CLASS];
double fBookPrice;
int iNewLine;


printf( &quot;Enter the book title: &quot; );
fgets( sBookTitle,MAX_TITLE,stdin );

iNewLine = strlen( sBookTitle )-1;
Remove_NewLine( sBookTitle,iNewLine );

printf( &quot;Enter the pub: &quot; );
fgets( sBookPub,MAX_PUB,stdin );

iNewLine = strlen( sBookPub )-1;
Remove_NewLine( sBookPub,iNewLine );

printf( &quot;Enter the price:$ &quot; );
fgets( sBookPrice,MAX_PUB,stdin );

iNewLine = strlen( sBookPrice )-1;
Remove_NewLine( sBookPrice,iNewLine );

fBookPrice = atof( sBookPrice );

printf( &quot;Is the book F or N?: &quot; );
fgets( sBookClass,MAX_PUB,stdin );

iNewLine = strlen( sBookClass )-1;
Remove_NewLine( sBookClass,iNewLine );


printf( &quot;\nTitle : %s &quot;,sBookTitle );
printf( &quot;\nPublisher : %s&quot;,sBookPub );
printf( &quot;\nPrice : $%.2f&quot;,fBookPrice );
printf( &quot;\nBook genere: %s\n\n&quot;,sBookClass );

}


void Remove_NewLine( char *sBuffer,int iIndex ) {

if ( sBuffer[iIndex] == '\n' )
sBuffer[iIndex] = '\0';

}



Darr01:You should try and stay away from gets(), for it is possible to overrun the buffer that is recieving the input. This is because gets() provides no bounds checking. When you call fgets(), it requires a max. length argument. This would be a safer bet. You just have to tell fgets that you want to read from stdin. As far as the price goes, just input it as a string and have atof() convert it into a double value. Also, you may want to add code to pull out all the excess char's from stdin if the user types more then the char array can hold. If you don't, then the input will spill into the other char buffers.
Mike L.G.
mlg400@blazemail.com
 
Hi!

Coould anyone tell me the use of Link List in C/C++ with example. I'm facing lot of errors while making a programme of Link List.

Also help me for adding/retriving data from file.


thank

--Alindra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top