Welcome to Dream.In.Code
Become a C++ Expert!

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!




gets() vs scanf()

 
Reply to this topicStart new topic

gets() vs scanf()

aaru_89
14 Oct, 2006 - 09:10 AM
Post #1

New D.I.C Head
*

Joined: 26 Sep, 2006
Posts: 20



Thanked: 1 times
My Contributions
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();
    
}
    


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Gets() Vs Scanf()
14 Oct, 2006 - 09:54 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Gets() Vs Scanf()
14 Oct, 2006 - 09:57 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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();
    
    return 0;    
}
    

User is offlineProfile CardPM
+Quote Post

aaru_89
RE: Gets() Vs Scanf()
14 Oct, 2006 - 10:28 AM
Post #4

New D.I.C Head
*

Joined: 26 Sep, 2006
Posts: 20



Thanked: 1 times
My Contributions
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

Thank You. Have a nice day.


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Gets() Vs Scanf()
14 Oct, 2006 - 10:34 AM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

aaru_89
RE: Gets() Vs Scanf()
14 Oct, 2006 - 10:39 AM
Post #6

New D.I.C Head
*

Joined: 26 Sep, 2006
Posts: 20



Thanked: 1 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Gets() Vs Scanf()
14 Oct, 2006 - 10:42 PM
Post #7

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
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".
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:46PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month