English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2 answers

void readNumbers(ifstream &refVar,int [] a, int s){
ifstream inputFile;
inputFile.open("numbers.txt");

// write code to read numbers from the file here

inputFile.close();
}

For starters, this function doesn't read any numbers.
The code would be something like

for ( int i = 0; i < s; ++i){
inputFile >> a[i];
}

The function must be called with parameters as well (stream to the file, pointer to the array, and array size); in your main, it's not.

2007-12-07 06:26:06 · answer #1 · answered by jgoulden 7 · 1 0

http://www.thescripts.com/forum/forum129.html
ask ur question on here its a c/c++ fourm

2007-12-07 13:12:54 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers