Solved

Reading in a struct

Posted on 2000-05-09
8
409 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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…
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 opening and reading files in the C programming language.

809 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