Solved

Reading in a struct

Posted on 2000-05-09
8
407 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:SaraBiz
Comment Utility
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
Comment Utility
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
Comment Utility
Is there anything else you wanted to discuss or work on before you grade this answer?
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 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

12 Experts available now in Live!

Get 1:1 Help Now