problem reading txt file

So I'm to open a txt file i have in my temp folder and copy the numbers into an array then take those numbers and calculate the lowest, highest, total, and average of that set of numbers. I think i'm having a problem copying the txt file into my array, i cannot see what is wrong, if you do please assist me :)


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int getLowest(int[]);
int getHighest(int[]);
int getTotal(int []);
int getAverage(int [], int);

int main()
{
int lowest, highest, total, average;
int number[11];
char fileName[20];
ifstream inFile;

cout << "Enter name of file: ";
cin >> fileName;

inFile.open(fileName);

for (int count = 0; count < 11; count ++)
{
inFile >> number[count];
cout << number[count] << endl;
}

inFile.close();

lowest = getLowest(number);
highest = getHighest(number);
total = getTotal(number);
average = getAverage(number, total);

cout << "The lowest number in the array is: " << lowest << endl;
cout << "The highest number in the array is: " << highest << endl;
cout << "The total of hte numbers in the array is: " << total << endl;
cout << "The average of the numbers in the array is: " << average << endl;

return 0;
}

int getLowest(int number[11])
{
int lowest;
lowest = number[11];
for (int count = 0; count < 11; count++) //Finds lowest value and returns
{
if (number[count] < lowest)
lowest = number[count];
}
return lowest;
}

int getHighest(int number[11])
{
int greatest;
greatest = number[11];
for (int count = 0; count < 11; count++) //Finds highest value and returns
{
if (number[count] > greatest)
greatest = number[count];
}
return greatest;
}

int getTotal(int number[11])
{
int total = 0;
for (int y = 0; y <= 11; y++)
total += number[y];

return total;
}

int getAverage(int number[11], int total)
{
int average;
average = (total/12);

return average;
}
You need to convert the Filename string into a c string you do this by doing
Filename.c_str()
EDIT: i have added part of a program i wrote a while ago that uses this method to create a file name of the users choice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool runprogram(){// this is part of a class, so savefile is not declared in the functions but it's declaration is std::ofstream savefile; 
        savefile.exceptions(std::ofstream::failbit | std::ofstream::badbit); //declaring exceptions 
        std::cout << "Enter a title for your webpage: ";
        getline(std::cin, filename);
        std::string hold; // used to create the full filename including path. 
        hold = filename; // tranfer name of file
        filename = "/Users/home/Desktop/dreamweaver/dreamweaver/"; // sets file path
        filename += hold; // now its filepath + filename
        filename += ".html"; // now its filepath + filename + file type
        try{
            savefile.open(filename.c_str()); //  safely opens file
        }
        catch (std::ofstream::failure &e){ // gets any exception and handles safely
            std::cout << "An error has occured"; // i do not care about specific error at this time, can add that later if desired  
            return false;  // ends program with false, saying that the program could not execute 
        }
Last edited on
Topic archived. No new replies allowed.