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
#include <iostream>
#include <fstream>
usingnamespace 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);
}
}