give an error if input file doesnt contain real numbers

Hello,
im trying to make a program that reads real numbers line by line in an "input.dat" file (The file contains only the values to be read and has a single value per line) and prints them to the screen. i will make some calculations after that, but for now i want to add some codes which provides the program to give an error if data file contains something different than real numbers. For example;
aa
bb
cc
12
23

give an error.

6
76.4
34
56.67
work fine.

here is my code:

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
#include <iostream>
#include <fstream>

using namespace std;

#define MAX_VALUES 1000

void printNumbers();

int main ()
{

printNumbers();

}

void printNumbers()
{
ifstream input("input.dat",ios::in);
input.clear();
input.seekg(0);

float Num;
float numbersIn[MAX_VALUES];
int counter=0;
    // If we can read good
    if (input.good()) {
         
         // Input The Numbers from The File Into The Array NumbersIn[]
         // While i < MAX_VALUES And While File Is Not End Of File.
        while (!input.eof() && (counter < MAX_VALUES)) {
            input>>Num;
            numbersIn[counter]=Num;
            counter++;
        }

        printf("\nThe Numbers Taken From The File Are :- \n");
        printf("---------------------------------------- \n");

        // Loop through the array to print the numbers
        for (int i = 0; i < counter; i++) {
            cout << numbersIn[i] << endl;
        }
    }
    else { printf("\nError with the Data File :\n");
           printf("Make Sure that Your Data File`s Type is '.dat'???\n");
           printf("Or Check Your Data File is in the Same Directory with Programme???\n");
           exit(1);
    }
}
You can test the failbit of the input file stream to catch the error
This article could be useful : http://www.cplusplus.com/forum/articles/6046/
Topic archived. No new replies allowed.