help with reading a structure

This is a discussion on help with reading a structure within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdio.h> #include <stdlib.h> #define MAX 20 typedef struct { int ID; char* firstname; char lastname[MAX]; float ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    help with reading a structure

    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 20
    
    typedef struct
        {
             int    ID;
             char*  firstname;
             char   lastname[MAX];
             float  unit;
             double gradeAvg;
        }INFO;
    
    int main (void)
    {
        // Global declarations
           INFO myInfo;
        int i = 0;
    
        // Statements
            myInfo.firstname = (char*)calloc(20,sizeof(char));
            printf("Enter your first and last name:\n");
        for(i = 0; i < MAX; i++)
        {
                scanf("%s %c", myInfo.firstname, &myInfo.lastname[i]);
        }
    
    
        printf("Enter your ID number:\n");
        scanf("%d", &myInfo.ID);
        printf("Enter your total unit completed:\n");
        scanf("%f",&myInfo.unit);
        printf("Enter your grade point average:\n");
        scanf("%lf", &myInfo.gradeAvg);
    
        i = 0;
        for(i = 0; i < MAX; i++)
        {
           printf("%9s %c\n", *myInfo.firstname, myInfo. lastname[i]);
        }
        printf("%6d\n", myInfo.ID);
        printf("%.1f\n",myInfo.unit);
        printf("%lf\n", myInfo.gradeAvg);
    
        return 0;
    }
    can i get a little help on correctting the loop over scanf
    thank you

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Whoa! What are you doing in your struct definition? You've got the firstname element defined as a pointer to a character- that only guarantees you've got room for that one character.

    You want both firstname and lastname to be strings (ie. arrays of characters) right? Well, declare them in your struct to be that, and then scanf them as strings. You shouldn't need a loop at all.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    well my instructor ask to dynamically allocate the firstname though
    so shouldn't it be pointer to char ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by khoavo123
    well my instructor ask to dynamically allocate the firstname though
    so shouldn't it be pointer to char ?
    Yes. That part is fine though slightly pointless when you're just going to make it 20 characters large anyway. Plus, you need to free what you calloc.

    Why exactly are you looping from 0 to MAX? Are you aware that myInfo is not an array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Yes, you're right. I didn't see the calloc. What that does is make firstname a pointer to a continuous block of memory, the size of twenty chars. It makes it an array of characters and it's no different than if you'd declared it the same way as lastname.

    You still shouldn't need a loop for the lastname. You can read that in all in one go, just the same as firstname. What the loop is making me do is type in my entire first name 20 times and then individual characters in my last name. So if my name is Joseph Bloggs I have to do this:
    Code:
    Joseph B
    Joseph l
    Joseph o
    Joseph g
    and so on.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    so after i make changes to this
    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 20
    
    typedef struct
        {
             int    ID;
             char*  firstname;
             char   lastname[MAX];
             float  unit;
             double gradeAvg;
        }INFO;
    
    int main (void)
    {
        // Global declarations
           INFO myInfo;
    
    
        // Statements
        myInfo.firstname = (char*)calloc(20,sizeof(char));
    
        printf("Enter your first and last name:\n");
        scanf("%s %s", myInfo.firstname, &myInfo.lastname);
        printf("Enter your ID number:\n");
        scanf("%d", &myInfo.ID);
        printf("Enter your total unit completed:\n");
        scanf("%f",&myInfo.unit);
        printf("Enter your grade point average:\n");
        scanf("%lf", &myInfo.gradeAvg);
    
    
    
        printf("%9s %19s\n", *myInfo.firstname, myInfo. lastname);
        printf("%6d\n", myInfo.ID);
        printf("%.1f\n",myInfo.unit);
        printf("%lf\n", myInfo.gradeAvg);
    
        system("PAUSE");
        return 0;
    }
    the program has no problem input data but it cannot printout data

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    This is wrong:
    Code:
    scanf("%s %s", myInfo.firstname, &myInfo.lastname);
    It should be:
    Code:
    scanf("%s %s", myInfo.firstname, myInfo.lastname);
    (but then this leaves you vulnerable to buffer overflow)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure and reading from a file
    By acmarshall in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2011, 02:40 PM
  2. Reading text file into a structure
    By jb1989 in forum C Programming
    Replies: 1
    Last Post: 12-13-2010, 11:17 AM
  3. Reading structure from file
    By nime in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2010, 04:17 PM
  4. reading structure from file..??
    By CyC|OpS in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 05:28 AM
  5. reading a string into a structure
    By breed in forum C Programming
    Replies: 1
    Last Post: 01-16-2002, 02:34 PM

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