I am supposed to write a program that asks the user for a file name. The program should read the contents of the file and display the following data:
the lowest number, the highest, the total of the numbers, and the average of the numbers.
My program will compile but it never reads the numbers from the file. It just gives me random numbers. My teacher wants us to use functions for everything, but I don't get what to do in the function readNumbers. My teacher completely left us on our own to do this. He just walked out of class so can anyone please help me with this?
2007-12-07
06:18:30
·
2 answers
·
asked by
USAman
6
in
Computers & Internet
➔ Programming & Design
#include
#include
using namespace std;
void readNumbers(ifstream &, int [], int);
int getLowest (int [], int);
int getHighest (int [], int);
int sumArray (int [], int);
double getAverage (int [], int);
const int ARRAY_SIZE = 12;
int main()
{
int numbers[ARRAY_SIZE];
int total;
double average;
int highest;
int lowest;
int userFile;
ifstream inputFile;
cout << "Please enter the file name: ";
cin >> userFile;
void readNumbers();
if (!inputFile)
{
cout << "Error opening the file.\n";
return 0;
}
2007-12-07
06:19:11 ·
update #1
lowest = getLowest(numbers, ARRAY_SIZE);
highest = getHighest(numbers, ARRAY_SIZE);
total = sumArray(numbers, ARRAY_SIZE);
average = getAverage(numbers, ARRAY_SIZE);
cout << "The lowest number is: " << lowest << endl;
cout << "The highest number is: " << highest << endl;
cout << "The total of the numbers is: " << total << endl;
cout << "The average of the numbers is: " << average << endl;
9 minutes ago
return 0;
}
//**************************
//Definition of readNumbers*
//**************************
void readNumbers(ifstream &refVar,int [], int)
{
ifstream inputFile;
inputFile.open("numbers.txt");
inputFile.close();
}
2007-12-07
06:19:49 ·
update #2
//************************
//Definition of getLowest*
//************************
int getLowest(int array[],int size)
{
int lowest;
void readNumbers();
lowest = array[0];
for (int count = 1; count < size; count++)
{
if (array[count] < lowest)
lowest = array[count];
}
return lowest;
}
//*************************
//Definition of getHighest*
//*************************
int getHighest(int array[], int size)
{
int highest;
void readNumbers();
highest = array[0];
for (int count = 1; count < size; count ++)
{
if (array[count] > highest)
highest = array[count];
}
return highest;
}
//***********************
//Definition of sumArray*
//***********************
int sumArra
8 minutes ago
int sumArray(int array[], int size)
{
int total = 0;
void readNumbers();
for (int count = 0; count < size; count++)
total += array[count];
return total;
}
2007-12-07
06:20:20 ·
update #3
//*************************
//Definition of getAverage*
//*************************
double getAverage(int array[], int size)
{
double average;
int total = 0;
void readNumbers();
for (int count = 0; count < size; count++)
total += array[count];
average = total / ARRAY_SIZE;
return average;
}
2007-12-07
06:20:48 ·
update #4
I have already created my numbers 1-12 list file in note pad as numbers.txt, so of course it already has a file of numbers to read from.
2007-12-07
06:40:17 ·
update #5