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;
}