Thread: structure member values

  1. #1
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    structure member values

    Can some one help me out plz
    this program has two structures the first is a file that has valid census iformation in it, and the 2nd is to display information based on the first struct.
    My problem is how can i gain the percentage of 1 to 2 occupants
    that live in a street.
    as you can't logically compare using the assertion or equality operators.



    struct data {
    char str_name[20];
    int hse_num;
    int occups;
    };

    struct totals {
    char str_name[20];
    int num_of_hses;
    int total_occups;
    int av_num_of_occups;
    float per_1-2_occups;
    float per_3-4_occups;
    float per_5+_occups;
    };

    void main()
    {
    FILE *fp;
    struct data *fp1, *fp2;
    int count=0;

    if( (fp = fopen(“CENSUS.DAT”, “rb”) ) == NULL)
    {
    printf(“Unable to open file”);
    return;
    }

    while( strcmp(fp1->str_name, “\0”) )
    {
    fp2 = fp1 + 1;
    count++;

    while(strcmp(fp2->str_name, “\0”) )
    {
    if( strcmp(fp1->str_name, fp2- >str_name) ==1)
    accum_totals();

    //other coding in here


    }
    fp2++;
    }
    fp1++;
    void display_info();
    count=0;
    }



    void accum_totals(struct totals *in_str)
    {
    int
    strcpy(data.str_name, total.str_name)
    num_of_hses = count;
    total_occups = data.occups;
    av_num_of_occups = data.occups / count
    per_1-2_occups????



    frustrated

    breed



    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The code has some errors.

    void main() { .... }

    This is an error. Your point of entry should look like:
    Code:
    int main()
    {
       //code goes here
       return 0;
    }
    I can see a little bit of logic but I don't think it works. You have to scan the records into variables before you do any comparisons on data.

    struct data record

    //open file
    int counter = 0;
    //part of necessary code
    while( fscanf("%d%s%s", &record.num_of_hses, ...) != EOF)
    {
    //increment counter each cycle
    }

    //counter is the number of records

    Basically I'm a little lost with what you are trying to accomplish with this code. It does not make much sense at this stage.

    Might help to show what the datafile looks like.

  3. #3
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    RE structure member comparisons

    Sorry about the crap code that i sent in.
    But I'm going to attemp to explain myself a bit more clearly.
    so plz forgive me if i get some of the jargon wrong

    I am trying to write a program that has a file already validated and is sorted by street name-
    struct census {
    char street_name[20];
    int house_number;
    int occupants;
    };

    void main()
    {
    int count=0; //using count as iteration marker
    {
    //.................................................. ......................
    ..........................some code
    }
    Now from this struct i need to colate the following info,

    street name which is easy strcpy(census.street_name, new_struct.street_name);

    the number of houses in a street, the total number of occupants that live on a street and the average number of occupants per house. This i thought i could do through a function.
    this i think i can work out.
    But (the big but!!)

    I also need the percentage of 1-2 people living in a house
    percentage of 3-4 " " " "
    percentage of 5+ " " " "

    Any idea's are all welcome

    can i get the percentages by using the comparison <>operators or == operators
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Start like this:

    Code:
    //header files
    struct census
     { 
         char street_name[20]; 
         int house_number; 
         int occupants; 
    }; 
    
    //notice it's int main, not void main
    int main() 
    { 
        struct census record[50];
         FILE *fptr;
         int count=0; //using count as iteration marker
         //open the file
         fptr = fopen("filepath","r");
         if(fptr == NULL) exit(1); 
         while(fscanf(fptr, "format specifiers", variables) != EOF)
         { 
         //load records
         //........................................................................ 
         ..........................some code
        //count += 1; //increment count
         } 
         //Now from this struct i need to colate the following info, 
         return 0;
    }
    You are required to load the data from the file into variables (ie records) in order to make comparisons on it. If you do not understand this from this code than you need to read several chapters such as: 'file I/O' and 'structures'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member of a structure that is a stucture member
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 11-29-2008, 10:33 AM
  2. Assignment to a structure array member
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 11:13 PM
  3. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  4. Allocating memory for a structure member
    By dalek in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2003, 06:56 AM
  5. File access question
    By Imperito in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2002, 11:37 AM