Thread: Beginners problem with C - User input, average etc

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Beginners problem with C - User input, average etc

    I got a problem with this program. It's supposed to get the number of family members from the user and their ages. It should then calculate the average age of the family, it only works when I use 2 as the number of family members. All the other times it doesnt work, I'm thinking it has something to do with the age variable's value being overwritten with the new one in the loop.

    Thoughts on how to fix it?

    Code:
    // This is a small little application that will give you the average age of your family
    // Written by Glasvegas
    // 23 Mar 2011
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(int argc, char *argv[])
    
    {
    
        int age = 0;
        int family = 0;
        int count = 1; //The question variable is set to 1 so the family member numbers start at 1
        int average;
    
        printf ("Please enter the number of members in your family: "); // Asks the user to enter the number of members in their family
        scanf ("%d", &family); // Takes the number the user entered and stores it into the family variable
    
        while (count < family + 1) { // As long as the variable question is less than the variable family this loop will continue
        printf("Enter the age of family member nr %d: ", count); // Asks the user to enter the age of family member nr x
        scanf("%d", &age);// Takes the number the user enters and stores it into the variable age
        age += age;
        count++; // This is so the loop will end
    
        }
    
         average = age / family; // Method to get the average of something
         printf("The average age of your family is: %d", average); // Outputs the answer
    
        getch();
        return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        scanf("%d", &age);// Takes the number the user enters and stores it into the variable age
        age += age;
    You are reading into "age", then basically doubling it. Then you read into age again, removing what was there and putting in a new value, then you double it.

    You should read into something else, and add that to your sum.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
        scanf("%d", &age);// Takes the number the user enters and stores it into the variable age
        age += age;
    You're trying to use the same variable to do two things. Create a second variable to store the running total instead (i.e. age_total += age). And then use that variable in calculating the average as well (average = age_total / family).

    EDIT: Beaten to the punch.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Thanks alot guys, it worked!
    I've followed thenewboston but he stopped making guides on C so now I need something new to learn from. Any suggestions for someone that is new with C?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a Solid Square based on user input
    By ThatBoy1124 in forum C Programming
    Replies: 3
    Last Post: 03-01-2011, 10:07 PM
  2. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. Need help managing user input
    By JayDiddums10 in forum C Programming
    Replies: 2
    Last Post: 11-19-2006, 05:01 PM
  5. Replies: 4
    Last Post: 04-21-2004, 04:18 PM