PDA

View Full Version : c code help



I_royalty_I
12-13-2014, 08:21 PM
This program is pretty sloppy, but at this point I don't really care haha. It function, so far, how I need it to.
I'm having one issue with it that I cant figure out, though.

Basically it asks for family members names, ages, locations, then calculates the average age and outputs those who are in texas.

I need help with the /printing out those who are in texas/ part.



#include <stdio.h>

int main()


{


int i, j, k, l=0, Age = 0;
int FamilyAges[5]={0};
char FamilyMembers[5];
char Name, Location;
char FamilyLocations[5];
float TotalAge = 0;
float NumFamily = 0;
float AverageAge;


for (i=0; i<5; i++)
{
printf("Input the name of the family members: \n");
scanf("%s", &Name);
FamilyMembers[i]=Name;
}

for (i=0; i<5; i++)
{
printf("Input the age of the family members: \n");
scanf("%d", &Age);
FamilyAges[i]=Age;
TotalAge = TotalAge + Age;
NumFamily++;
}

for (i=0; i<5; i++)
{
printf("Input the Location of the family members: \n");
scanf("%s", &Location);
FamilyLocations[i]=Location;
}



printf("The total number of family members is: %f\n", NumFamily); /*for testing purposes */
printf("The total age of family members is: %f\n", TotalAge); /*for testing purposes*/
AverageAge = (TotalAge / NumFamily);
printf("The average age of the family members is %f\n", AverageAge);


return 0;


}



I tried to add in something like


while (l <= NumFamily)
{
if (FamilyLocations[l] = "Texas")
{
printf("The family member %s is from Texas\n", FamilyMembers[l]);
}
}

But I think I screwed something up, or don't have it done right.
Suggestions? D:

I_royalty_I
12-13-2014, 11:12 PM
Revised my code a bit. Not sure if my arrays were initiated properly or not. But they should be now if they weren't before.
Still have the same issue though.


#include <stdio.h>

int main()


{


int i = 0;
int FamilyAges[5]={0};
char FamilyMembers[5];
char FamilyLocations[5];
float TotalAge = 0;
float NumFamily = 0;
float AverageAge;


while (i<5)
{
printf("Input the name of the family members: \n");
scanf("%s", &FamilyMembers[i]);
i++;
}

i=0;

while (i<5)
{
printf("Input the age of the family members: \n");
scanf("%d", &FamilyAges[i]);
TotalAge = FamilyAges[i] + TotalAge;
NumFamily++;
i++;
}

i=0;

while (i<5)
{
printf("Input the Location of the family members: \n");
scanf("%s", &FamilyLocations[i]);
i++;
}

i=0;

printf("The total number of family members is: %f\n", NumFamily); /*for testing purposes */
printf("The total age of family members is: %f\n", TotalAge); /*for testing purposes*/
AverageAge = (TotalAge / NumFamily);
printf("The average age of the family members is %f\n", AverageAge);


return 0;


}

Zachafer
12-20-2014, 02:48 PM
I tried to add in something like


while (l <= NumFamily)
{
if (FamilyLocations[l] = "Texas")
{
printf("The family member %s is from Texas\n", FamilyMembers[l]);
}
}

But I think I screwed something up, or don't have it done right.
Suggestions? D:

You can't compare strings with == or != in C. You have to use strcmp


if (strcmp(FamilyLocations[l], "Texas") != 0)
{
printf("The family member %s is from Texas\n", FamilyMembers[l]);
}I think that should work