Thread: Problem with gets

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    Problem with gets

    Hello Everyone!
    The program is not waiting for me to enter the name,it prints name and address printf statements and then pauses,how do I correct this?

    insert
    Code:
    void phone_book(int o)
    {
        printf("enter the name\n");
        gets(c[o].name);
    
    
        printf("enter the address\n");
        gets(c[o].addr);
    
    
        printf("enter the email id \n");
        gets(c[o].e_id);
    
    
        printf("enter th phone no\n");
        scanf("%d",&c[o].ph_no);
    
    
    }
    output:
    enter the name
    enter the address

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The newline remains in the input stream. Try doing a:
    Code:
    while ((c = getchar()) != '\n' && c != EOF);
    That should clear the input buffer( c should be "int" ).

    EDIT: FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    Last edited by GReaper; 02-28-2012 at 06:59 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Or just use fgets(). It's much safer and you don't have to worry about newline character issues.

    Also, the structure of that program (global struct array that's not a pointer) looks wasteful.

  4. #4
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Thanks for the link Greaper.
    fgets is working fine,but before that I use scanf for switch input in the main function and by this even if I use fgets in the above (phone_book) function,it's still printing the two lines together.

    insert
    Code:
    int main() { int ch=1; printf("PHONE BOOK\n"); printf("Menu\n"); printf("1.Write 2.Show 3.Search 4.Edit 5.Exit\n"); scanf("%d",&ch); // problem part,when I comment this part,it works smoothly,what else can I use instead of this? switch(ch) { case 1 : phone_book(1); break; default : printf("invalid"); break; } return 0; }




  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shruthi View Post
    fgets is working fine,but before that I use scanf for switch input in the main function and by this even if I use fgets in the above (phone_book) function,it's still printing the two lines together.
    Because when you enter a single char, what you actually enter is two chars -- a letter and '\n' for the return key. Scanf(%c) reads the first one, and as GReaper says, the second one remains in the stdin buffer. Fgets() reads upto and including a '\n', and that's what happens next.

    Look at the code suggested in post #2 to deal with this issue, think about how it works, and apply it appropriately.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by GReaper View Post
    The newline remains in the input stream. Try doing a:
    Code:
    while ((c = getchar()) != '\n' && c != EOF);
    That should clear the input buffer( c should be "int" ).

    EDIT: FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)

    Tried using while statement and the stored in variable c was 10 instead of 1,which was the user input,therefore it went to the default statement of the switch case.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shruthi
    Tried using while statement and the stored in variable c was 10 instead of 1,which was the user input,therefore it went to the default statement of the switch case.
    Read GReaper's post again. What is that while loop supposed to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by laserlight View Post
    Read GReaper's post again. What is that while loop supposed to do?
    Isn't it,variable c should take a value other than '\n' and EOF and when it when it receives '\n' or EOF it exits the while loop?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're missing the point: the use of scanf leaves the trailing newline in the input buffer.

    The idea then is to remove this newline. There are a few ways, and this while loop is one of them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Thanks Everyone I finally solved it with all your help. The mistake I made when I added the while loop was,I used the same variable in scanf and in the while loop,that's why it was not working.When I used two separate variables it worked fine.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shruthi
    One more thing,the while loop should be without the semicolon at the end,only then it works.
    Err... no. That semi-colon is deliberate as the loop body does no other work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void phone_book()
    {
        char name[5],addr[6],e_id[7];
        int ph;
        printf("enter the name\n");
        fgets(name,sizeof(name),stdin);
    
    
        printf("enter the address\n");
        fgets(addr,sizeof(addr),stdin);
    
    
        printf("enter the email id \n");
        fgets(e_id,sizeof(e_id),stdin);
    
    
        printf("enter th phone no\n");
        scanf("%d",&ph);
    
    
    }
    
    
    int main()
    {
        int ch,c;
         printf("PHONE BOOK\n");
            printf("Menu\n");
           printf("1.Write 2.Show c.Search d.Edit e.Exit\n");
           scanf("%d",&ch);
           while ((c = getchar()) != '\n' && c != EOF)
            switch(ch)
            {
                case 1 : phone_book();
                            break;
                default : printf("invalid");
                            break;
            }
        return 0;
    }
    For some reason,this code is working properly without the semicolon,please have a look.

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ideally, it works, but try pressing the spacebar a few times after you enter your selection and before hitting enter and observe what happens.
    Devoted my life to programming...

  14. #14
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Sorry for the trouble,but it seems to work even without the semicolon,but I leave it at what GReaper has commented,while loop with semicolon works well.

  15. #15
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by GReaper View Post
    Ideally, it works, but try pressing the spacebar a few times after you enter your selection and before hitting enter and observe what happens.
    Yeah,I got the point thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM