Member Avatar for mgirl

Hi I'm quite new to writing programs in C language. I've been working hard on a program and my output isn't coming out as it should.
Problem is you're supposed to enter 7 numbers. The program should sort the numbers in 5 categories of ages. Then calculate the average. I only get 0's and garbage for the average.
Sorry as this is homework if it offends anyone, but I can't see what's wrong.
could anyone point me in the right direction?

#include<stdio.h>

int main ()
{ 
    int age, min, ret ,cent ,sum, invalid, total ,cptmin=0, cptret=0, cptcent=0,cptinvalid=0
     ,i=1;
    float avg;
    
    for( i=1; i<=7; i++);
         printf("Enter the age of 7 people:", age);
         scanf("%d)", &age);
         i++;
         if( age>0 && age<19){
             min++;
             }
         else if (age>55 &&age<120){
             ret++;
             }
         else if (age>=100 && age<120){
             cent++;
             }   
         else if(age<1 && age >120){
             invalid++;
             }
            
             total=min+ret+cent;
             avg= totale/i++;
         
         
         printf("\n Number of under age people      :%d", cptmin);
         printf("\n Number of retirees              :%d", cptret);
         printf("\n Number of people over 100       :%d", cptcent);
         printf("\n Number of invalid entries       :%d" cptinvalide);
         printf("\n Average age                     :%d", avg);
         scanf("%.2f",&avg);
         
         getch ();
         }

thank you for any ideas

Recommended Answers

All 3 Replies

You have not initialised min, ret, cent, or invalid, which means their starting values are undefined (ie can contain random junk). Then, in your loop, you increment them. You then add the resultant values to compute total .....

Starting with junk values and incrementing yields more junk. Adding junk values yields further junk

Also, before you ask other questions in this forum, have a look at this message. In particular, note the need to use code tags, in order to format code in a way that is human readable.

> I only get 0's and garbage for the average.
Strange, I see several typos which would stop the program from compiling, nevermind running.

Did you copy/paste the source code from your editor (which would be good) ?
Or did you just retype the code here based on what you thought you have (which would be worse than useless) ?

> avg= totale/i++;
For example, there is no 'e' in total, not as you've declared it anyway.

> for( i=1; i<=7; i++);
Despite your indentation, none of the following code executes 7 times with that trailing ; on the for loop.
Even after you remove the training ;, you need to add a pair or { } to make your intented loop explicit.

Or did you just retype the code here based on what you thought you have (which would be worse than useless) ?

Yeah, I noticed that too. I assumed the post was just typing what she remembered having, as the code could never compile, so just focused on "uninitialised variables yield crap results, even if code is otherwise correct". Cumulatively, a combination of the types of errors Salem and I have pointed out would explain what's happening.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.