SaraBiz
asked on
Reading in a struct
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==-9 999)
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].annu al_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>av g)
{
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_me m-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);
}
#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==-9
i=25;
}
for(i=0; i<*ctr+1; i++)
{
scanf("%lf", &family[i].annual_inc);
if(family[i].annual_inc==-
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;
total=total+family[i].annu
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;
if(family[i].annual_inc>av
{
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;
{
pov_lvl=7500+950*(family[i
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That'd be great! How would I go about it...is there a debugger I could download or something?
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?
ASKER
I'm just using whatever compiler the college system used...I believe its unix based...and its truly awful!
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.
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.
Is there anything else you wanted to discuss or work on before you grade this answer?
ASKER
sorry about that! forgot i still hadn't graded it yet
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.