simply question, structures

This is a discussion on simply question, structures within the C Programming forums, part of the General Programming Boards category; so simple, but i found so many different ways to do it, but whats the easiest all i want to ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    18

    simply question, structures

    so simple, but i found so many different ways to do it, but whats the easiest
    all i want to do is take info and put it in the stucture
    Code:
    #include <stdio.h>
    
    struct info { 
    
    int ss_number; 
    char name[15]; 
    
    }
    
    
    i want to:
    scanf("Please enter your ss number:");
    gets(info.ss_number);
    scanf("Please enter your name:";
    gets(into.name)
    
    printf("Your ss number is: %i", info.ss_number);
    printf("Your name is: %s", info.name);
    whats wrong with that?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    printf() is for output, scanf() is for input, and don't use gets(), use fgets().

    Read this tutorial for more info.

    gg

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    It seems to me your trying to pass data into a structure definition. You must declare an instance of the structure first.

    Try this :

    structure definition

    struct information {

    int ss_number;
    char name[15];

    };


    delcare an instance of structure

    struct information info;

    Then try passing the data into the structure

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Read this too.

    gg

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    Info is the name of your structure, not an instance of it. You need to declare an instance of it, for example in say main(), do something like.
    Code:
    struct info employee;
    Now memory is allocated for 1 info structure called employee. So to access say the ss_number you can simply do
    Code:
    employee.ss_number
    There are a few other ways to make your structures to, and you should be aware of them all to avoid confusion. You can do this.
    Code:
    typedef struct {
                /* variables here */
    } EMPLOYEE;
    Here you create a new structure type I guess yuou could call it.So you can say.
    Code:
    EMPLOYEE Joe;
    And then to access it simple say
    Code:
    Joe.ss_number;
    And finally, this can also be done.
    Code:
    typedef struct info {
               /* variables here */
    }EMPLOYEE;
    This gives you flexibility, now EMPLOYEE is a new type you created. Its a synonym for struct info. So now to create an instance of it you have Two options. You can say.
    Code:
    struct info Joe;
    /* or */
    EMPLOYEE Joe;
    And to access one of its members just
    Code:
    Joe.ss_number
    Hope that helps, goodluck.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    18
    so...

    Code:
    #include <stdio.h>
    
    struct information { 
    
    int ss_number; 
    char name[15]; 
    };
    
    int main()
    {
            struct information person;
    
    printf("Please enter your ss number:");
    scanf("%ld", &person.ss_number);
    printf("Please enter your name:";
    scanf("%s", person.name);
    
    printf("Your ss number is: %i", info.ss_number);
    printf("Your name is: %s", info.name);
    }
    hows them apples?

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    18
    i forgot the & on the second scanf

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    18
    i forgot a ) on the second printf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to structures - Beginner question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-10-2006, 05:57 PM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  3. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 05:28 AM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  5. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 06:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21