Results 1 to 3 of 3

Thread: c code help

  1. #1
    I_royalty_I's Avatar
    Joined
    Dec 2011
    Posts
    7,028
    Userbars
    78
    Thanks
    6,794
    Thanked
    10,970/3,916
    DL/UL
    30/0
    Mentioned
    1,998 times
    Time Online
    437d 22h 11m
    Avg. Time Online
    2h 29m

    c code help

    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.






    I tried to add in something like

    Code:
    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:
    Last edited by I_royalty_I; 12-13-2014 at 08:40 PM.
    What's my definition of success?
    Creating something no one else can
    Being brave enough to dream big
    Grindin' when you're told to just quit
    Giving more when you got nothin' left

  2. #2
    I_royalty_I's Avatar
    Joined
    Dec 2011
    Posts
    7,028
    Userbars
    78
    Thanks
    6,794
    Thanked
    10,970/3,916
    DL/UL
    30/0
    Mentioned
    1,998 times
    Time Online
    437d 22h 11m
    Avg. Time Online
    2h 29m
    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.

    Code:
    #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;
    
    
    }
    What's my definition of success?
    Creating something no one else can
    Being brave enough to dream big
    Grindin' when you're told to just quit
    Giving more when you got nothin' left

  3. #3
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    Quote Originally Posted by I_royalty_I View Post
    I tried to add in something like

    Code:
    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

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •