Solved

Reading in a struct

Posted on 2000-05-09
8
411 Views
Last Modified: 2008-02-20
Hi! I've finally gotten this program to compile, but something is quite wrong.  I run it, and nothing happens! I do run it using a data file for the input, but nothing happens.  Any ideas?  Thanks!


#include <stdio.h>

typedef struct
{
int id_no;
int house_mem;
double annual_inc;
} household_t;


void get_data(household_t *family, int *ctr)

        {
        int i;

        /* assuming user input is correct- same # of each type */


        for(i=0; i<25; i++)
                {
                scanf("%d", &family[i].id_no);
                   *ctr++;
                if(family[i].id_no==-9999)
                        i=25;
                }

        for(i=0; i<*ctr+1; i++)
                {
                scanf("%d", &family[i].house_mem);
                if(family[i].house_mem==-9999)
                        i=25;
                }

        for(i=0; i<*ctr+1; i++)
                {
                scanf("%lf", &family[i].annual_inc);
                if(family[i].annual_inc==-9999)
                        i=25;
                }

}                        


   void print_table(household_t *family, int ctr)

{
int i;

printf("Identification\t Annual\t Household\n");
printf("Number\t\t Income\t\t Members\t\t");
for(i=0; i<ctr; i++)
printf("%d\t\t %f\t\t %d\n", family[i].id_no,
family[i].annual_inc, family[i].house_mem);

}


void calc_avg(household_t *family, int ctr)
{
double total=0, avg;
int above, i;

for(i=0; family[i].annual_inc!=ctr; i++)
total=total+family[i].annual_inc;      
   avg=total/i;
printf("The average family income is %.2lf.\n\n", avg);
printf("The following families have incomes above this average:\n");

for(i=0; family[i].annual_inc!=ctr; i++)
if(family[i].annual_inc>avg)
{
printf("%d\t$%d\n", family[i].id_no, family[i].annual_inc);

above++;
}
printf("There is a total of %d families who earn above the average.", above);
}

void poverty_lvl(household_t *family, int ctr)
{
double per_pov;
int pov_lvl, i, numbelow=0;

for(i=0; family[i].annual_inc!=ctr; i++)
{                                      

         pov_lvl=7500+950*(family[i].house_mem-2);
if(pov_lvl>0)
numbelow++;
}
per_pov=numbelow/i*100;
printf(" %.2lf percent have incomes below the poverty level.",
per_pov);
}


int main(void)
{
household_t family[25];
int ctr=0;


get_data(family, &ctr);

print_table(family, ctr);

calc_avg(family, ctr);        
 
poverty_lvl(family, ctr);
}  


0
Comment
Question by:SaraBiz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 2793697
The first, and probably the critical error lies in the first loop.

  for(i=0; i<25; i++)
  {
      scanf("%d", &family[i].id_no);
      *ctr++;
      if(family[i].id_no==-9999)
          i=25;
  }

The statement "*ctr++" is presumably intended to increment the counter ctr. However what it in fact does is increment the pointer. This is because ++ binds tighter than *. So the overall result of the statment is as if it is bracketed like this *(ctr++); (Not like this (*ctr)++).

The solution, obviously, is to write the statement as indicated in the negative example i.e.

      (*ctr)++;
0
 
LVL 16

Expert Comment

by:imladris
ID: 2793718
I feel compelled to again point out, that this problem became readily apparent when a debugger was run on the code. (And I didn't spot it before either). It went through the first loop successfully, but skipped the second one without executing a single iteration. This lead to an inspection of ctr (since it controls the loop termination), which showed it to be 0, which led to watching its behaviour in the course of the execution of the first loop.

The same can be accomplished (though not so conveniently) with print statements.

In other words, it seems to me that the real task here, is to teach you how to debug code. And I am willing to help in the context of this forum.
0
 

Author Comment

by:SaraBiz
ID: 2793745
That'd be great! How would I go about it...is there a debugger I could download or something?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:imladris
ID: 2793804
Whether a debugger is available or not depends on what compiler you are using. Even if there isn't one, though, code can be debugged with printf statements. What compiler are you using?
0
 

Author Comment

by:SaraBiz
ID: 2793831
I'm just using whatever compiler the college system used...I believe its unix based...and its truly awful!
0
 
LVL 16

Expert Comment

by:imladris
ID: 2793960
Then printf statements is probably what you're left with. In this particular scenario, the first question was: why is nothing printing? To discover that I would have put printf statements in the print_table function. A first one announcing that the function had been entered, another one for each iteration of the loop, and another one at the end.

Presumably you would have gotten a line for entry and exit, but none for the loop. This would lead to adding a print statement for the ctr variable which would turn out to be 0. Then you would go back to the first loop in getdata to find out why ctr didn't increment the way you wanted it to. etc. etc.
0
 
LVL 16

Expert Comment

by:imladris
ID: 2801144
Is there anything else you wanted to discuss or work on before you grade this answer?
0
 

Author Comment

by:SaraBiz
ID: 2802478
sorry about that! forgot i still hadn't graded it yet
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
voltage to force translation ? 8 101
C simple state machine for client server handshake 4 415
Problem to save 10 174
What is sub-make ? 2 88
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question