I'm trying to write a program that takes numbers from a file that contains numbers and letters. The first two numbers of the file tell me how many rows and columns there are. The rest of the numbers will be stored in a 2-D array. I managed to get it working for the most part. The only thing I'm trying to do now is display an error message if there is a letter at any place in the file. How would I do this? Here's what I have so far. I'm trying to find a way to detect the letter and display an error message. The letter could be anywhere in the file (the beginning, or even directly next to another number)
cout << "Enter the full name of the file (with extension)" << endl;
cin >> file;
//Check if the file is valid
inFile.open(file.c_str());
if (!inFile)
{
cout << "ERROR: File cannot be opened" << endl;
return 0;
}
//Take out the first two values in the file for the rows and columns
inFile >> rows;
inFile >> columns;
//Check the size of the array, no more than 20 rows/columns
if (rows < 0 || rows > MAX || columns < 0 || columns > MAX)
{
cout << "ERROR: Invalid amount of rows/columns in the file." << endl;
return 0;
}
//Store the rest of the file here
double full[rows][columns] = {0};
while(inFile)
{
for(int ro=0; ro < rows; ro++)
{
for (int co=0; co < columns; co++)
{
inFile >> full[ro][co];
}
}
break;
}
The while loop is pointless as you will have to iterate through all the rows and columns before checking the validity of inFile. Likewise with the break statement.
The only thing I'm trying to do now is display an error message if there is a letter at any place in the file. How would I do this?