I am trying to write a code that will input name age and state of family members
ID: 3764418 • Letter: I
Question
I am trying to write a code that will input name age and state of family members, then print the average age and family that live in Texas only. Can anyone tell me where I am going wrong with this code?
#include <stdio.h>
//Average age function
float Avg_age(float total_age, float total_count)
{
float Avg_age;
Avg_age = total_age/total_count;
return Avg_age;
}
int main(void) {
// declare variables
char F_Name, State;
float total_age, Aver;
int age, count, input;
float total_count;
//initialize variables
count=0;
total_age=0.0;
//Ask user how many family members they want to input
printf("How many family members do you want to add? ");
scanf("%d",&input);
//loop
while (count<input)
{
printf("Please enter the name, age and state of family member. ");
scanf("%s",&F_Name);
scanf("%d",&age);
scanf("%s",&State);
//scanf("%s %d %s",&F_Name, &age, &State);
total_age+=age;
count++;
while(State='TX')
{
printf("%s lives in Texas ",F_Name);
}
}
else
{
printf("Input limit reached ");
}
//Call the average age function
Aver=Avg_age(total_age, total_count);
printf("Average age of family is:%.1f ", Aver);
//printf("%d",count);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
//Average age function
float Avg_age(float total_age, float total_count)
{
float Avg_age;
Avg_age = total_age/total_count;
return Avg_age;
}
int main(void) {
// declare variables
char* F_Name;
char* State;
float total_age, Aver;
int age, count, input;
float total_count;
//initialize variables
count=0;
total_age=0.0;
//Ask user how many family members they want to input
printf("How many family members do you want to add? ");
scanf("%d",&input);
//loop
while (count<input)
{
printf("Please enter the name, age and state of family member. ");
gets(F_Name);
scanf("%d",&age);
gets(State);
//scanf("%s %d %s",&F_Name, &age, &State);
total_age+=age;
count++;
while(strcmp(State,"TX"))
{
printf("%s lives in Texas ",F_Name);
}
}
else
{
printf("Input limit reached ");
}
//Call the average age function
Aver=Avg_age(total_age, total_count);
printf("Average age of family is:%.1f ", Aver);
//printf("%d",count);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.