Solved

Reading in a struct

Posted on 2000-05-09
8
408 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Do email signature updates give you a headache?

Are you constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now