I'm trying to make a small program that gives you the average age of your whole family. I'm into arrays in my book, and I'm trying to experiment with stuff. My thoughts behind the program are this....
1. Get total number of family members.
2. get ages of all the members and insert it into an array.
3. Total up all the ages inside the array and insert that total into a new variable.
4. Send the two variables to a function do the actual averaging and post a witty comment.
Code:
//This is going to be a practice program that averages the age of your family
#include <iostream>
using namespace std;
/*
familyfunct is supposed to take the 2 variables from the main function, divide them into
a third variable average. then run a series of if loops dependant on averages number.
*/
int familyfunct(int combinedages, int famtot, int average){
cout<<"About to do the math.....\n\n\n";
average = (combinedages/famtot);
cout<< average << endl;
if(average>80){
cout<<"You're all old!";
}
if(average< 20){
cout<<"You're a young family!";
}
if(average< 30){
cout<<"You're getting older!";
}
else{
cout<<"You're pretty normal";
}
}
int main(){
int ages[100]; //this array holds the ages of all family members up to 100 people.
int FamiTotal, AverAge;
int i; //for for loop
cout<< "How many family members do you have?";
cin>>FamiTotal;
cout<<"Enter each family members age. Press enter when complete.";
cin>>ages[]; //this is supposed to insert each family member into the ages[] array
/*
This for loop is supposed to extract the data from the array ages[] then
add them all up. This is the main problem of the program.
*/
for(i=0; i<100; i++){
AverAge= ages[i]+ ages[i];
}
familyfunct(AverAge, FamiTotal); // sends AverAge and FamiTotal
return 0;
}
Here is what I came up with. I'm new to arrays, and my book doesn't seem to go too much into them right now, so I'm experimenting.
The bolded area of my code is the area that I need help on. How do I extract the list of numbers from my array and then add them up to put that total into a new variable? I figured it'd be some type of for loop, but I don't really know.
Secondly, I'm getting some compiler errors that seem unrelated to array issue I'm having. Can't quite figure them out, but here they area...
Code:
C:\Users\justin\C++\My Code\arrays1.cpp||In function 'int main()':|
C:\Users\justin\C++\My Code\arrays1.cpp|43|error: expected primary-expression before ']' token|
C:\Users\justin\C++\My Code\arrays1.cpp|10|error: too few arguments to function 'int familyfunct(int, int, int)'|
C:\Users\justin\C++\My Code\arrays1.cpp|50|error: at this point in file|
||=== Build finished: 3 errors, 0 warnings ===|
Well, any help is appreciated! This program is just me experimenting since I'm a self learner. I don't really have anyone to go to but you guys and my pdf.