Thread: gets() skipped!

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    gets() skipped!

    I try to do linked list for inserting. But when i try the gets(newPtr->name), it never work. It just skip off. Also , the code i posted here are reduced to the most simple + working because it's an assignment and I dont want to be in trouble.

    Code:
    newPtr = malloc (sizeof(node));  //Create a node with newPtr pointing to it
    
    do{
            //I skip some syntax which is crucial in linked list........<--bcause //it's an assignment
    
            printf("Name:");
            gets( newPtr -> name);//not working
            printf("Course: ");
            gets( newPtr -> course);
            printf("ID: ");
            scanf("%d", &newPtr -> id);
            printf("Score: ");
            scanf("%f", &newPtr -> score);
            printf("=================================================\n");
            printf("Do you wish to continue? 'S' to STOP:");
            scanf(" %c", &status);
            printf("================================================\n");
        }while(status != 'S'); //continue loop while state is not 'N'

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because scanf leaves the newline in the buffer. Anyway, don't use gets. Use say, fgets instead, to avoid buffer overflow.
    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

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Changed. Still no hope...haiz

    Code:
    newPtr = malloc (sizeof(node));  //Create a node with newPtr pointing to it
     
    do{
            //I skip some syntax which is crucial in linked list........<--bcause //it's an assignment
     
            printf("Name:");
            fgets( newPtr -> name,30,stdin);
            printf("Course: ");
           fgets( newPtr -> course,30,stdin);
            printf("ID: ");
            scanf("%d", &newPtr -> id);
            printf("Score: ");
            scanf("%f", &newPtr -> score);
            printf("=================================================\n");
            printf("Do you wish to continue? 'S' to STOP:");
            scanf(" %c", &status);
            printf("================================================\n");
        }while(status != 'S'); //continue loop while state is not 'N'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I already gave you the explanation. Now, time for you to do some debugging, so you'll always remember this. Using your debugger, examine the contents of newPtr->name when you encounter the "skip off". What do you observe?
    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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    oo. so that's means u want me to use fgets to get the sentences and wanted to remove the \n character, right.

    Using your debugger, examine the contents of newPtr->name when you encounter the "skip off". What do you observe?
    it already got input earlier than the next one. (probably \n) character.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    you can write the following function and call it before calling the gets function.

    Code:
    clear_input_buffer();
    gets(string);
    The function clear_input_buffer is as follows:
    Code:
    int clear_input_buffer(void)
    {
    	int ch;
    	while(((ch=getchar())!=EOF) && (ch != '\n'));
    	return ch;
    }

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    I try this . Anyway still no hope.

    Code:
    while(tempChar = getchar() != '\n'){
                newPtr -> name[i++] = tempChar;
     }
            newPtr -> name[i++] = '\0';

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    aw still not working even with scanf. I try many many alternate way but it's still not working.

    Code:
      do{
                scanf("%c", newPtr -> name[i]);
                i++;
            }while(newPtr -> name !='\n');

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kappilrinesh View Post
    you can write the following function and call it before calling the gets function.
    Don't use gets! Why do you insist on it when laserlight explicitly mentioned that it should not be used?

    ncode, the idea here is that you call clear_input_buffer before your scanf statements. You did use the debugger, right? You understand the issue, right?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    ncode, the idea here is that you call clear_input_buffer before your scanf statements. You did use the debugger, right? You understand the issue, right?
    That would be bad, you'd end up consuming valid input since the problem shows up after scanf executes and is followed up with gets (or fgets).

    The scanf function leaves any input it did not scan in for other functions to get. The gets function follows its own rules. It stores input in the argument until the newline character is read, and it is disposed of. If there is stuff in the buffer for gets to read in, then the user doesn't have an opportunity to type.

    The fix is to follow up scanf calls with anything that discards input. Further reading can be found here.

  11. #11
    Registered User BinaryBlock's Avatar
    Join Date
    Feb 2012
    Location
    Michigan
    Posts
    6
    Are you Linux? You never specified. if so:

    Code:
    system("/bin/stty raw");
            selection=getchar();
            system("/bin/stty cooked");

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Use one mor gets at the and of the loop
    for example i use always this

    Code:
    char error[50];
    char surname[25];
    char name[20];
    int id;
    
    while(1) //this is an example
    { 
      printf... enter name;
      gets(name);
     
      printf... enter surname;
     gets(surname);
    
    printf...enter id;
    scanf("%d",&id);
    
    gets(error); //this will copy the id value(you dont have to enter any value). if you dont put this, in the next time of looping, name will be skipped.
    }
    
    ....
    i never used fgets etc. dont know how it works, i used it only at file openings.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rac1 View Post
    Use one mor gets at the and of the loop
    No! Don't use gets!

    i never used fgets etc. dont know how it works, i used it only at file openings.
    Not a valid excuse. Go learn it and come back, then.
    Stop teaching newbies bad practices.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    if it works no problem doesn't it?
    yes works...

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It works no problem?

    Code:
    #include <stdio.h>
    struct foo {
       char s1[5];
       char s2[5];
    };
    
    int main(void) 
    {
       struct foo bar = { "" , "" };
       gets(bar.s1);
       printf(" s1 = \"%s\"\n s2 = \"%s\"\n", bar.s1, bar.s2);
       return 0;
    } 
    
    /*
    C:\Documents and Settings\User>a
    12345678
     s1 = "12345678"
     s2 = "678"
    */
    Clearly this is just flawless... why was s2 allowed to be changed?

    fgets is not rocket science:
    Code:
     resultptr = fgets( storage, howmanycharacters, fromfile );
    It's easy to use and safe, please figure it out or just stop using C...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf Being skipped
    By GibsMe in forum C Programming
    Replies: 2
    Last Post: 10-11-2009, 03:23 PM
  2. Scanf() skipped
    By new-b in forum C Programming
    Replies: 9
    Last Post: 07-18-2009, 01:34 AM
  3. scanf is being skipped
    By yougene in forum C Programming
    Replies: 6
    Last Post: 12-24-2008, 06:05 AM
  4. Second scanf keeps getting skipped.
    By yougene in forum C Programming
    Replies: 9
    Last Post: 12-23-2008, 02:39 AM
  5. Why is this statement being skipped?
    By TalonStriker in forum C Programming
    Replies: 22
    Last Post: 11-12-2007, 05:45 AM