Thread: gets(); function not working..

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    18

    Question gets(); function not working..

    Hey there, I have this problem with the gets();. My problem is that the gets(); only gets the the "Category's", and it does not allow the user to input the "Term's". Can you guys help me with this? I would be very happy if you can help me.. I think it has something to do with the function addTopic.. i'm sorry guys because it is my first time to use the gets(); because my teacher required me to..
    Code:
    #include <stdio.h>
    struct infopairs/*Max up to 15 information pairs
    usr should not be asked how many information pairs will be inputed.*/
    {
       char datadesc[50];/*Only one word needed*/
       char dataval [150];/*Can be more than 1 word*/
    };
    struct theTopic/*This one consists of the following:
    Term, Category and Information Pairs. This structure will
    be used to add a topic.*/
    {
       char term[50];/*More than 1 word, 50 characters*/
       char category[50];/*More than 1 word, 50 characters*/
    
    
       struct infopairs descval;/*I called this desc for description
       and val for value*/
    };
    void addTopic(struct theTopic *info)/*This one is for adding a topic.
    Called the theTopic struct and named it as "info". I put the pointer
    so that it will not be "passed by value".*/
    {
    
    
       system("cls");/*It means that the system will clear the screen*/
    
    
       printf("                     Adding Topic\n");/*This will be shown to users.*/
       printf("\n");
       printf("Please enter the term:  \n");/* Tell user to enter term*/
       gets(info->term);
       printf("Enter the category:     \n");/*Ask for the category*/
       gets(info->category);
    }
       int main()
    {  struct theTopic info;/*Declare info, info is a structure*/
    
    
       int choice;/*the choice of the user from the menu*/
    
    
       /*This is the menu*/
       printf("          Quiz Bee\n"
             "____________________________\n"
             "Choose the option:\n"
             "[1] Add Topic\n"
             "[2] Add Information\n"
             "[3] Delete Topic\n"
             "[4] Delete Information\n"
             "[5] View All Topics\n"
             "[6] View By Category\n"
             "[7] View Specific Term\n"
             "[8] Export\n"
             "[9] Import\n"
             "[0] Exit\n");
    
    
       scanf("%d", &choice);/*Get user's choice*/
    
    
       /*User's choice will have outcomes by using switch*/
    
    
       switch(choice)
       {
              case 1: system("color 2b");
                      addTopic(&info);/*addTopic will be used if '1' is pressed.
              Address of info will be passed on to the addTopic.*/
                      break;
              case 2: break;
              case 3: break;
              case 4: break;
              case 5: break;
              case 6: break;
              case 7: break;
              case 8: break;
              case 9: break;
              case 0: break;
       }
    
    
       system("pause");/*pauses system*/
       return 0;/*Returns a 0*/
    }
    
    
    
    }
    Last edited by cloudpuffballz; 11-01-2012 at 10:49 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The problem has nothing to do with using gets(). That code won't compile, since you have implemented the main() function within the body of addTopic(). Such nesting of functions is illegal in C.


    If your teacher is advocating usage of gets(), consider the quality of the teaching questionable. gets() is deprecated (scheduled for removal from a future version of the C standard) because it is one of the most dangerous and easily misused functions there is. Using the system() function is also highly dubious (the effects are operating system dependent), although not quite as dangerous as the gets() function.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    Are there any ways to make the gets(); make the user input the string? Because my teacher said that if I wanted the user to enter more than one string, then I must use the gets(); function..


    oh yeah, I used Codeblocks for this, but it compiled..

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by grumpy View Post
    gets() is deprecated (scheduled for removal from a future version of the C standard)
    gets() is not described in the C11 Standard.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    I see.. is there any better way to get the user's input of more than one string?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Marvee. Another clown who edits his code after receiving an answer, so it looks like the answers received were meaningless.

    Try describing the symptoms you are getting. If the code (now) compiles, does the compiler issue any warnings? When you run it, what input are you supplying, and what behaviour do you see?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    I run the program again, and then I pressed '1' and then entered, then it showed something like this:

    Please enter the term:
    Enter the category:
    _ <---- I can only input here

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    I run the program again, and then I pressed '1' and then entered, then it showed something like this:

    Please enter the term:<---- Skipped
    Enter the category:
    _ <---- I can only input here

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    Oh yeah, sorry about editing a while ago, i had forgotten to add some stuff in the code that's why I edited it.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    This code is weird, but it kinda worked..
    Maybe the code doesn't get the input when you use gets(); for the first time?????
    Code:
    void addTopic(struct theTopic *info)/*This one is for adding a topic.
    Called the theTopic struct and named it as "info". I put the pointer
    so that it will not be "passed by value".*/
    {
    
    
       system("cls");/*It means that the system will clear the screen*/
    
    
       printf("                     Adding Topic\n");/*This will be shown to users.*/
       printf("\n");
       gets(info->term);
       printf("Please enter the term:  \n");/* Tell user to enter term*/
       gets(info->term);
       printf("Enter the category:     \n");/*Ask for the category*/
       gets(info->category);
    }

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yeah, okay. The problem is that you are mixing scanf() calls with gets(). scanf("%d", &choice) stops reading when it encounters whitespace. So when you enter a 1 followed by the enter key, it will read the value 1 and leaves the carriage return to be read. The next gets() call reads that carriage return character - which it interprets as meaning a line has been entered, and returns. Hence the first gets() call after the scanf() gives an empty string.


    The usual recommendation is not to mix formatted I/O (eg scanf()) with line-oriented I/O (like gets()) on the same stream because of that sort of interaction between styles of input. Similarly, it is not recommended to mix formatted or line-oriented input with character-oriented input (eg getchar()) on the same stream, because of other interactions.

    What you need to do is (if you must really use gets()) is use gets() to read every line of input. Instead of using scanf("%d", &choice) use
    Code:
        char buffer[100];    /*  assumes input will never exceed 99 characters */
        gets(buffer);
        sscanf(buffer, "%d", &choice);     /*   Note scanning a string */
    This will interact as you expect with subsequent calls of gets(), since it does not attempt to read integral values directly from the standard input device (eg user input).



    Note, however, that it is STILL a really bad idea to use gets() at all.
    Last edited by grumpy; 11-01-2012 at 11:50 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    18
    I see, thanks for helping.. i'll really try not to use the gets(); anymore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By Tom.b in forum C Programming
    Replies: 8
    Last Post: 10-05-2009, 01:12 PM
  2. Another function not working...
    By Inti in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 11:40 AM
  3. Function not working
    By Inti in forum C Programming
    Replies: 2
    Last Post: 02-07-2009, 09:48 AM
  4. why this function is not working
    By chintugavali in forum C++ Programming
    Replies: 6
    Last Post: 01-07-2008, 12:35 AM
  5. Function not working! :*(
    By mramazing in forum C Programming
    Replies: 7
    Last Post: 01-07-2007, 02:14 AM

Tags for this Thread