school assignment finding average

My problem is I keep getting zero as an answer in my program. The program is supposed to add five numbers and give the average. We are required to have 3 files like I have included an average.h average.cpp and main.ccp files. All I need is a little pointer as to why I keep getting a 0 as an answer. I think I have been staring at this code way to long lol


1
2
3
4
//Average.h
#include<iostream>
using namespace std;
float calcAverage (int numberInputs[], const int size);




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// main.cpp file
#include "average.h"
using namespace std;
int main()
{
int numberInputs [5];
int userInput;
int size = 5;
cout <<"This program will average a group of 5 numbers of your choice." <<endl;
cout <<"\n\nPlease enter 5 numbers: " <<" ";
cin >> userInput;
cout <<"\nThe average of the numbers you entered is: ";
cout << calcAverage(numberInputs, size) <<endl; 
system("pause");
return 0;
      }




1
2
3
4
5
6
7
8
9
10
11
12
13
//average.cpp file
#include "average.h"
float calcAverage(int numberInputs[], const int size)
{
int sum = 0;
int i = 0;
float average = sum / size; //to find average
for (int i = 0; i < size; ++i)
{
sum += numberInputs[i]; //calculation to get the total sum of numbers from user
}
return average;
}



2 problems I see.

The data the user is inputing is going into userInput which is a single integer.
Your passing numberInputs (array of 5 integers) into calcAverage. You need to get the user input into the array before calling calcAverage.

Also inside calcAverage your doing
float average = sum / size; //to find average
before the for loop, should be after.
Topic archived. No new replies allowed.