Hi, I am a beginner and have written a simple program that reads data from a file and tells the user what the highest value read in was, and the lowest value read in was. For the test file, I used a simple text file with a few lines of random numbers, and it compiled and worked just fine. However, when I add something non-numerical to the file to test my code for errors, my program simply won't open the file. For example, if I added a line of text that said 'hi', for some reason my program won't even open the file. I'll include my code below so you can see it
// A simple program that simply finds the highest value, and the lowest value in the file provided by the user
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace std;
staticdouble findLowest (char filePath[]);
staticdouble findHighest (char filePath[]);
int main ()
{
char filePath[80];
cout <<"This program will find the highest and lowest numerical value in the file you\nprovide\n" <<endl;
cout << "Please enter a path to the file you wish to open" << endl;
cin >> filePath;
cout <<"\nThe highest numerical value in this file is " << findHighest(filePath) << endl;
cout <<"The lowest numerical value in this file is " << findLowest(filePath) << endl;
system ("PAUSE");
return 0;
}
// the findHighest Function
// Finds the highest value in a file of numbers
// Parameters: a char array, which is the file path to the file which contains the numbers
// Returns: a double, which is the highest numerical value in the file
// Pre-conditions: the file is formatted as such: there is one number per line
staticdouble findHighest (char filePath[])
{
double highestValue = -DBL_MAX;
double input;
int lineCount = 1;
ifstream externalData;
externalData.open (filePath);
if (externalData.fail ())
{
cout <<"Could not open the file" << endl;
}
else
{
while (!(externalData.eof()))
{
externalData >> input;
lineCount++;
if (externalData.bad())
{
cout << "error on line " << lineCount <<", not a valid numerical value" << endl;
externalData.clear();
}
if (input > highestValue)
{
highestValue = input;
}
}
}
return highestValue;
}
// the findLowest Function
// Finds the lowest value in a file of numbers
// Parameters: a char array, which is the file path to the file which contains the numbers
// Returns: a double, which is the lowest numerical value in the file
// Pre-conditions: the file is formatted as such: there is one number per line
staticdouble findLowest (char filePath[])
{
double lowestValue = DBL_MAX;
double input;
int lineCount = 1;
ifstream externalData;
externalData.open (filePath);
if (externalData.fail ())
{
cout <<"Could not open the file" << endl;
}
else
{
while (!(externalData.eof()))
{
externalData >> input;
lineCount++;
if (externalData.bad())
{
cout << "error on line " << lineCount <<", not a valid numerical value" << endl;
externalData.clear();
}
if (input < lowestValue)
{
lowestValue = input;
}
}
}
return lowestValue;
}
I'm not sure what it's happening I don't have too much time right now, but... You should close the file after using it, and trying to get the max value of letters doesn't make sense... You was previously reading number, now you would be reading letters.
Also in findHighest () I think you're returning the maximum representable finite floating point number if exist in the file, not the highest value readed, vice versa withfindLowest (). But again I didn't look at it too much...