Thread: Help on accessing structures using 'gets'

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Unhappy Help on accessing structures using 'gets'

    This is a cut from my code:


    ##########################################
    Code:
    /* Database (name,age,salary,height,weight) program using structure and 
       gets. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #define MAX 4 /* To define MAX as 4 */
    
    int main()
    {
      /* Database structure definition. */
    typedef struct input{
            char name[256];
            int age;
            int salary;
            float height;
            float weight;
            }Input; /* End structure database. */
      Input info[MAX];      /* New type name for struct database. */
      int x,age,salary,height,weight;   /* initialize variables. */
      char name[256];   /* initialize characters. */
    
      /* Loop through database. */
      for(x=0;x<MAX;x++)
      { 
      printf("\n  Please type full name of person %d \t\t", x+1);
      gets(info[x].name);   
      printf("\n  Please type age of person %d \t\t", x+1);
      scanf("%d", &info[x].age);
      printf("\n  Please type salary of person %d \t\t", x+1);
      scanf("%d", &info[x].salary);
      printf("\n  Please type height of person %d \t\t", x+1);
      scanf("%f", &info[x].height);
      printf("\n  Please type weight of person %d \t\t", x+1);
      scanf("%f", &info[x].weight);
      }
    ###########################################
    I need some answer on the bold area, thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I need some answer on the bold area, thank you.
    Don't use gets, it's impossible to make it safe. I'm not sure what kind of answer you were looking for since you didn't ask a question.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    My question is that I need to know how to use gets in this code, because I am told to use it by my lecture...

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Can anyone help me with this one please? I really need the answer because I've tried so many ways on doing it but it does not work. Thank you very much for your help.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I need to know how to use gets in this code
    That's simple, don't. But there's nothing "technically" wrong with how you're using it now. Did you test it?

    >because I am told to use it by my lecture...
    I'd ask you to smack him for me if I thought that you would. You should consider finding someone better qualified to teach you C.

    [edit]
    Or did you stumble on the whole scanf/unformatted input problem? It's asked often, and answered just as often if you want to search the forum for it.
    [/edit]
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    So there is no way using 'gets' to access structures? The lecture told me to use scanf for one code and 'gets' for another code. I have been searching sites by sites for an answer,

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    The problem of this code is that when i try to compile and run it, the second looping for the 'gets' is not working and the code jumps to 'scanf' for inputting age and so on. Why is it happenning?

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Sounds like a '\n' is being left in the input buffer.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Quote Originally Posted by pianorain
    Sounds like a '\n' is being left in the input buffer.
    This is not a problem. The problem is in the looping part, I can't figure out what's wrong and the code doesn't give me any error to debug when I compile and run it...

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So there is no way using 'gets' to access structures?
    Did you completely miss the part where I said that your code was correct?

    >Why is it happenning?
    Finally, you describe the real problem. It's exactly what I predicted. scanf doesn't play well with unformatted input functions because it usually treats whitespace differently. In this case, a newline character is being left in the stream by scanf and gets is reading it immediately and terminating successfully. This is a relevant FAQ entry, as is this.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    The following code sample works for me. All I did was force the floating point library to load with the dummy float variable and clear out the structure. I single stepped the code thru a debugger and the structure loads clean. Just the way you'd expect the structure to load.

    Bob

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <memory.h>
    /***************************************
    This program shall SORT!
    Made by Mark Crick
    ©2005 Mark Crick
    ****************************************/
    #define MAX 256
    
    
    int main(void)
    {
        float fDummy = 0.0; // Force the floating point library to load
        typedef struct input1{
            char name[256];
            int age;
            int salary;
            float height;
            float weight;
        }Input; /* End structure database. */
        Input info[MAX]= {0};      /* New type name for struct database. */
        int x,age,salary,height,weight;   /* initialize variables. */
        char name[256] ={0};   /* initialize characters. */
        /* Loop through database. */
        for(x=0;x<MAX;x++)
        { 
            printf("\n  Please type full name of person %d \t\t", x+1);
            gets(info[x].name);   
            printf("\n  Please type age of person %d \t\t", x+1);
            scanf("%d", &info[x].age);
            printf("\n  Please type salary of person %d \t\t", x+1);
            scanf("%d", &info[x].salary);
            printf("\n  Please type height of person %d \t\t", x+1);
            scanf("%f", &info[x].height);
            printf("\n  Please type weight of person %d \t\t", x+1);
            scanf("%f", &info[x].weight);
        }
        return 0;
    }

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    THanks everyone for your help! I finally finish the code. What I do is just by adding this code (while (getchar() != '\n') after the last scanf. Thank you again for your contribution guys, I appreciate them very much.

    Best Regards,

    phil1p

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. accessing an array of structures
    By creative in forum C Programming
    Replies: 4
    Last Post: 12-27-2007, 12:52 PM
  3. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  4. Accessing members of structures
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 02-03-2002, 12:10 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM