Find non-numbers in a file

So I have an input file like:

"1 2 3 4 A B C D"

The vector contains only the numerical values since it is reading them in as integers. However, how can I check the file for non-integers? For example, if it sees that "A", I want to throw an error and return -1.

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
  vector<double> nums;      // vector to hold numbers from the file.


bool Numbers::Read (istream& istr)  // read in the numbers from the file
{


ifstream inputFile("nums.txt");   // open the file for reading

if (inputFile)
{

float meanTotal = 0.0; // mean value
double result;	// result of the numbers being added together
int count;	// count the amount of items in the file

	while (inputFile >> count)	// read the numbers into the vector
	{
		
		nums.push_back(count);
		++count;
	
	}

	inputFile.close();
You may add something like this after the while loop:
1
2
if(!inputFile.eof()) // If not end of file the file coudn't be read entirely
  throw_error();


By the way: You cannot use count like that because on line 17 it's overwritten with the content of the file
Last edited on
Topic archived. No new replies allowed.