Program won't open file

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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

// 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>

using namespace std;
static double findLowest (char filePath[]);
static double 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
static double 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
static double 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;
}


What is the problem?
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...
No, I figured it out. Thanks
Topic archived. No new replies allowed.