C programming question

I have a task to create a program using C language. The program is Design a program that will allow a user to Input a list of your family members along with their age and state where they reside. Determine and print the average age of your family and print the names of anyone who lives in Texas.

I have done a lot to my code but I can’t seem to get the average age of the family to work. If I can get any help I would appreciate that. Here is my code so far. I am very new to this so anything would be great.

#include <stdio.h>
#include <string.h>

int main(void)
{
int names,age;
char state[50], name[50];
float sum, avg;

for (names=0; names<50; names++)
{
// reset Sum to 0
sum =0.0;

printf(“Enter a family members name:\n”, name);
gets(name);

printf(“The name you entered was: %s \n”, name);

printf(“Enter the abbreviation for the State that %s lives in (for example: MD, TX, CA, NY):\n”, name);
gets (state);

printf(“You entered %s \n”, state);

if ( strcmp(state,“TX”)== 0)
printf("%s is from Texas\n", name);
else
printf("%s is not from Texas… \n", name);

for (age=0; age <50; age++)

sum =0.0;

printf (“Enter Age: \n”);
scanf("%d", &age);

while (age<50)
avg = age/50;

printf( “Average age for family is %f \n”,avg);
}
return 0;
}

First of all decide the number of family members and sum their ages and at the end divide it by number of family members .

here is the example code snippet :-

printf(“enter the number of members\n”);

scanf("%d",&n);

int sum=0;

while(n–)
{

printf(“enter name\n”);

scanf("%s",name);

printf(“enter state\n”);

scanf("%s",state);

printf(“enter age\n”);
printf("%d",&age);

if ( strcmp(state,“TX”)== 0) printf("%s is from Texas\n", name); else printf("%s is not from Texas… \n", name);

sum=sum+age;

}
int avg_age=sum/n;
printf(“average age is %d”,avg_age);

Try this… Hope this is what you want…

#include <stdio.h>
#include <string.h>
void main()
{
int names,age,sum;
char state[50], name[50];
float avg;
clrscr();

for (names=0; names<4; names++) { // reset Sum to 0
sum =0;

printf(“Enter a family members name:\n”, name); gets(name);

printf(“The name you entered was: %s \n”, name);

printf(“Enter the abbreviation for the State that %s lives in (for example: MD, TX, CA, NY):\n”, name); gets (state);

printf(“You entered %s \n”, state);

if ( strcmp(state,“TX”)== 0) printf("%s is from Texas\n", name); else printf("%s is not from Texas… \n", name);
}

sum=0;
for (names=0; names <4; names++)
{
printf(“enter the ages:”);
scanf("%d",&age);
sum=sum+age;
}
avg=sum/4;
printf(“Average: %f”,avg);
getch();
}

@rocky9801

Can you please tell me the reason of using this loop?

for (age=0; age <50; age++)
sum =0.0;

I found a bug… but please let me know why did you use that loop, and can you please provide a sample input for 1 iteration

Coming on to bug:

When ever we scan a number either float or int, as soon we hit enter for input it acts like a ‘\n’ in buffer and scanf() search for this ‘\n’ ans as soon ‘\n’ is encountered scanf() is terminated leaving ‘\n’ as it is in buffer,

which create trouble while getting a string input right after scanning a numeric input.

This was just an explanation,

to prevent this, use getchar() right after scanning the numeric input.

In your code just add

getchar()

after

printf ("Enter Age: \n"); 
scanf("%d", &age);

so it should look like,

printf ("Enter Age: \n"); 
scanf("%d", &age);
getchar();

Hope this post is helpful for you… :slight_smile:

But still i have some doubts with your code if you’ll be able to provide a sample test case for single iteration i may able to correct your code it it is still not working… :slight_smile: