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

1. ## 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. 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.

Quzah.

3. 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.

4. 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?