Storing numbers in a 2-D Array from a file with numbers and letters

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)

File Example:
3 2
7 11.4
4.25 r
8 7.5

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

    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;
        }
Last edited on
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?
1
2
3
4
5
6
7
8
9
10
for(int ro=0; ro < rows; ro++)
{
    for (int co=0; co < columns; co++)
    {
        if( !(inFile >> full[ro][co]) ) {
            std::cout << "*** bad input! ***\n";
            break;
        }
    }
}
Topic archived. No new replies allowed.