Join 137,270 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,451 people online right now. Registration is fast and FREE... Join Now!
Hello. I'd written the following code for storing book details. I'm using scanf() in my code. Due to this, there is provision only for one word to be stored in a variable such as author's name. I tried using gets() for such variables which required to store more than one word, but there was some problem during data input by user which I'm not able to figure out why. Can someone please help? Thanks.
Here's my code:
CODE
#include<stdio.h> #include<conio.h>
struct { char name[50],aut[50],pub[50],gen[50]; int pg; float price; }book[25];
void main() { int i,n,r,r1; char c='y';
printf("\n Enter the number of books: "); scanf("%d",&n);
for(i=0;i<n;i++) { printf("\n Enter name of book:"); scanf("%s",book[i].name);
printf("\n Enter name of author: "); scanf("%s",book[i].aut);
printf("\n Enter name of publisher: "); scanf("%s",book[i].pub);
printf("\n Enter genre: "); scanf("%s",book[i].gen);
printf("\n Enter price of book: "); scanf("%f",&book[i].price);
printf("\n Enter number of pages: "); scanf("%d",&book[i].pg); }
while(toupper(c)=='Y') { printf("\n Which record do you wish to review? "); scanf("%d",&r1); r=r1-1;
printf("\n Name of the book: %s",book[r].name); printf("\n Name of the author: %s",book[r].aut); printf("\n Name of the publisher: %s",book[r].pub); printf("\n Genre: %s",book[r].gen); printf("\n Price: %.2f",book[r].price); printf("\n Number of pages: %d",book[r].pg);
printf("\n Do you wish to view another record?(Y/N): "); getchar(); c=getchar(); }
printf("\n Thank you. Have a nice day."); getch();
Well, you've not actually specified the problem you have encountered with gets(), so it's a little hard to diagnose. I would, however, advise agaist the use of the gets() function altogether...it is extremely susceptible to overflow, and cannot really be used safely. You may wish to try the fgets() function instead.
What was the problem you encountered with the gets() function?
fgets is the function you want to use to retreive data with spaces in them.
CODE
#include<stdio.h> #include<conio.h>
struct { char name[50],aut[50],pub[50],gen[50]; int pg; float price; }book[25];
int main() { int i,n,r,r1; char c='y';
printf("\n Enter the number of books: "); scanf("%d",&n); getchar();
for(i=0;i<n;i++) { printf("\n Enter name of book:"); fgets(book[i].name, 50, stdin);
printf("\n Enter name of author: "); fgets(book[i].aut, 50, stdin);
printf("\n Enter name of publisher: "); fgets(book[i].pub, 50, stdin);
printf("\n Enter genre: "); fgets(book[i].gen, 50, stdin);
printf("\n Enter price of book: "); scanf("%f",&book[i].price);
printf("\n Enter number of pages: "); scanf("%d",&book[i].pg); }
while(toupper(c)=='Y') { printf("\n Which record do you wish to review? "); scanf("%d",&r1); r=r1-1;
printf("\n Name of the book: %s",book[r].name); printf("\n Name of the author: %s",book[r].aut); printf("\n Name of the publisher: %s",book[r].pub); printf("\n Genre: %s",book[r].gen); printf("\n Price: %.2f",book[r].price); printf("\n Number of pages: %d",book[r].pg);
printf("\n Do you wish to view another record?(Y/N): "); getchar(); c=getchar(); }
printf("\n Thank you. Have a nice day."); getch();
The problem was, the first gets(),i.e, the one to receive the data for name of book did not get any value. Here's the sample output with gets().
CODE
Enter number of books: 1
Enter name of book: Enter name of author: martin
Enter name of publisher: chand and co
Enter genre: language
Enter price of book: 225.55
Enter number of pages: 500
Which record do you wish to review? 1
Name of the book: Name of the author: martin Name of the publisher: chand and co Genre: language Price: 225.55 Number of pages: 500 Do you wish to review another record?(Y/N): n
The reason that is happening is due to your first scanf prompting the user for number of books. scanf leaves in the input buffer the character for the Carrage return, you just need to have a getchar() after the scanf statement to clear the buffer. Then fgets and gets will work properly.
If you look at the code I provided in my previous response you will see I added one just after the first scanf prompt.
As Amedeus mentioned use the fgets instead of the gets function, the gets function is prone to overflow which can cause your program to crash if the user enters more characters than are in your array.
To tell the truth, I'm not at home using any function other than scanf() and printf(). Maybe I'll make use of other functions properly in future. Thanks for the help people.
I'd suggest you to use gets() and fgets() as well.
Not only to prevent Buffer Overflows, but when when you enter this string as input Welcome aaru_89 , scanf would store only "Welcome" and ignore the rest of the characters after the space.
gets() and fgets() on the other hand, would store the input as "Welcome aaru_89".