Hello,
This question pertains to a programming problem that I have stumbled across in my C++ programming class.
The Assignment:
-----------------------------------------------------------------------------------
Number Analysis Program
Write a program that asks the user for a file name. Assume the file contains a series of numbers, each written on a separate line. The program should read the contents of the file into an array and then display the following data:
The lowest number in the array
The highest number in the array
The total of the numbers in the array
The average of the numbers in the array
-----------------------------------------------------------------------------------
This is what I have so far and I don't know where to go from here to make i give a total sum of the numbers in the file or an average. I can't even get it to open the file in question, x.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main() {
int lowestNumber;
int highestNumber;
const int ARRAY_SIZE = 12;
int numbers [ARRAY_SIZE];
int index = 0;
int sum;
double avgNumber;
ifstream inFile;
string filename = "";
//Requests the name of the file the User wants to use.
cout << "Please give me the name of the file." << endl;
cin >> filename;
//Opens the file the User specified.
inFile.open(filename.c_str());
while (index < ARRAY_SIZE && inFile >> numbers[index]);
do
{
cin >> numbers[index];
cout << endl;
if(index == 12)
{
lowestNumber = numbers[0];
highestNumber = numbers[0];
}
if(lowestNumber > numbers[index])
lowestNumber = numbers[index];
if(highestNumber < numbers[index]);
highestNumber = numbers[index];
index++;
while(index < ARRAY_SIZE);
cout << "The lowest value in the document is:\n\n";
cout << "The highest value in the document is:\n\n";
int total = 0;
for(int index = 0; index < ARRAY_SIZE; index++)
sum += total;
inFile.close();
} while(index == 12);
return 0;
}
|