help with structure arrays

This is a discussion on help with structure arrays within the C Programming forums, part of the General Programming Boards category; I read the rules, and no i'm not asking anyone to do my homework...I want to know what i'm doing ...

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

    help with structure arrays

    I read the rules, and no i'm not asking anyone to do my homework...I want to know what i'm doing wrong with these structure arrays. I've tried to figure it out but i still can't get it. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    
    #define MAXSIZE 80
    
    typedef struct {
        char name[80];
        double diam;
        double dist;
        int moons;
    } myVar;
    
    
    void printPlanets( myVar planetInfo[] )
    {
        int i;
    
        for( i = 0; i < 8; i++ )
        {
            printf("Planet %d:\n", i);
            printf("\tname: %s\n", planetInfo[i].name);
            printf("\tdiameter: %g\n", planetInfo[i].diam);
            printf("\tdistance to the Earth: %g\n", planetInfo[i].dist);
            printf("\tnumber of moons: %d\n", planetInfo[i].moons);
        }
    }
    
    int main( int argc, char *argv[] )
    {
        myVar planets[7];
        int i;
        FILE *in;
    
        if( argc == 2 )
        {
            in = fopen(argv[2], "r");
        }
        else
        {
            in = stdin;
    
        for( i=0; i < 8; i++ )
        {
            printf("Please enter the name of a planet: ");
            fgets(planets[i].name, MAXSIZE, in);
            printf("Please enter the diameter of the planed (km.): ");
            scanf("%g", planets[i].diam);
            printf("Please enter the planet's distance from the Earth: ");
            scanf("%g", planets[i].dist);
            printf("Please enter the number of moons for the planet: ");
            scanf("%d", planets[i].moons);
    
            if( i == 7 )
            {
                printf("Thank you, you entered the following data:\n");
            }
        }
        }
    
        printPlanets(planets);
    
        return 0;
    }

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Didn't test ur code...just saw three lines and found 1 error...its string.h not strings.h

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    I've always used strings.h in my class.

    But that's not relevant, b/c i haven't put in the code yet that needs strings.h. I didn't use my MAXSIZE either...

  4. #4
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Here is the corrected code...u were missing & in scanf()...and i think u should again see ur for loop...are u sure it should be < 8
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXSIZE 80
    
    typedef struct {
        char name[80];
        double diam;
        double dist;
        int moons;
    } myVar;
    
    
    void printPlanets( myVar planetInfo[] )
    {
        int i;
    
        for( i = 0; i < 8; i++ )
        {
            printf("Planet %d:\n", i);
            printf("\tname: %s\n", planetInfo[i].name);
            printf("\tdiameter: %g\n", planetInfo[i].diam);
            printf("\tdistance to the Earth: %g\n", planetInfo[i].dist);
            printf("\tnumber of moons: %d\n", planetInfo[i].moons);
        }
    }
    
    int main( int argc, char *argv[] )
    {
        myVar planets[7];
        int i;
        FILE *in;
    
        if( argc == 2 )
        {
            in = fopen(argv[2], "r");
        }
        else
        {
            in = stdin;
    
        for( i=0; i < 8; i++ )
        {
            printf("Please enter the name of a planet: ");
            fgets(planets[i].name, MAXSIZE, in);
            printf("Please enter the diameter of the planed (km.): ");
            scanf("%g", &planets[i].diam);
            printf("Please enter the planet's distance from the Earth: ");
            scanf("%g", &planets[i].dist);
            printf("Please enter the number of moons for the planet: ");
            scanf("%d", &planets[i].moons);
    
            if( i == 7 )
            {
                printf("Thank you, you entered the following data:\n");
            }
        }
        }
    
        printPlanets(planets);
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    your scanfs are missing the address of operator&
    eg:
    Code:
    scanf("%d", &planets[i].moons);
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Quote Originally Posted by sunnypalsingh
    Here is the corrected code...u were missing & in scanf()...and i think u should again see ur for loop...are u sure it should be < 8
    ohh thanks. didn't see that missing &. And yeah, it needs to be <= 8 so it includes the 9th planet. Thanks alot, I"ll try those out. And why can't it be strings.h? I've never used string.h....

  7. #7
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by mapunk
    And why can't it be strings.h? I've never used string.h....
    What functions are u planning to use??

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    strlen and strcpy...they're in both strings.h and string.h


    k so now i have this code, and when I try to input the information, it only allows me to enter the planet.name once, but i can enter all the other information. Any ideas why? It'll just skip the planet.name query after i enter it the first time

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    
    #define MAXSIZE 80
    
    typedef struct {
        char name[MAXSIZE];
        double diam;
        double dist;
        int moons;
    } myVar;
    
    
    void printPlanets( myVar planetInfo[] )
    {
        int i;
       
        for( i = 0; i < 9; i++ )
        {
            printf("Planet %d:\n", i);
            printf("\tname: %s\n", planetInfo[i].name);
            printf("\tdiameter: %g\n", planetInfo[i].diam);
            printf("\tdistance to the Earth: %g\n", planetInfo[i].dist);
            printf("\tnumber of moons: %d\n", planetInfo[i].moons);
        }
    }
    
    int main( int argc, char *argv[] )
    {
        myVar planets[8];
        int i;
        FILE *in;
    
        if( argc == 2 )
        {
            in = fopen(argv[2], "r");
        }
        else
        {
            in = stdin;
    
            for( i=0; i < 9; i++ )
            {
                printf("Please enter the name of a planet: ");
                fgets(planets[i].name, MAXSIZE, in);
                printf("Please enter the diameter of the planed (km.): ");
                scanf("%g", &planets[i].diam);
                printf("Please enter the planet's distance from the Earth: ");
                scanf("%g", &planets[i].dist);
                printf("Please enter the number of moons for the planet: ");
                scanf("%d", &planets[i].moons);
    
                if( i == 7 )
                {
                    printf("Thank you, you entered the following data:\n");
                }
            }
        }
    
        printPlanets(planets);
    
        return 0;
    }
    Last edited by mapunk; 11-17-2005 at 06:08 AM.

  9. #9
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    scanf is causing problem for u...try clearing stream after last scanf.....u can use fflush(stdin);.....but this has a undefined behavior...should not be used for input streams.....but some compilers do support it...like VC 6.0.....
    maybe gurus can throw more light on it
    Last edited by sunnypalsingh; 11-17-2005 at 09:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zeroing out member arrays in a Structure
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2004, 03:50 AM
  2. Structure Within Structure
    By Shakira in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:35 PM
  3. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 11:45 AM
  4. displaying data from a structure of arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 03-14-2002, 12:35 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

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