nested for loops

I have to make this program input from a file "puzzle.txt" which contains the matrix size and word puzzle contents.

For example the input would be:
5 8
hptnrocr
aeehkcra
moaiauag
itmupcni
rauioibn

Right now I am trying to get it to print the puzzle to the screen but right now the console screen just pops up and disappears quickly. I am not sure what I am doing wrong. Is my nested for loop wrong?

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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include <fstream>

using namespace std;

const unsigned maxSize = 22;

void printPuzzle(char p[maxSize][maxSize]);

int main()
{
    const char* fileName = "puzzle.txt";

    ifstream inFile(fileName);
    if (!inFile.is_open())
    {
        cout << "Unable to open file \"" << fileName << "\"\n";
        return 0;
    }

    unsigned rows, columns;
    if (!(inFile >> rows >> columns))
    {
        cout << "Unable to read row/column information from \"" << fileName << "\"\n";
        return 0;
    }

    if (rows < 1 || rows > maxSize )
    {
        if ( rows < 1 )
            cout << "Row size of 0 is not allowed.\n";
        else
            cout << "Row size of " << rows << " is too large.\n";
        return 0;
    }

    if (columns < 1 || columns > maxSize)
    {
        if (columns < 1)
            cout << "Column size of 0 is not allowed.\n";
        else
            cout << "Column size of " << columns << " is too large.\n";
        return 0;
    }

    char puzzle[maxSize][maxSize];
    
    printPuzzle(puzzle);
    
    system("PAUSE");
    return 0;      
}

void printPuzzle(char p[maxSize][maxSize]){
     for(int r=0; r<maxSize; r++)
     {
         for(int c=0; c<maxSize; c++)
         {
             cout<<p[r][c]<<" ";
         }
         cout<<endl;
     }
}
Since you aren't ever hitting your system call (which you shouldn't use btw, it's not very safe), I can only guess that one of your other checks is failing and causing your program to return immediately.
so execute your program from a console.
Also, as it is now, `puzzle' has garbage (is used without being initialized)
Last edited on
How do I initialize "puzzle"?

where would my checks be failing?
I believe all that I need to figure out now is how to get my program to read and output the text from the file. How would I do this?
Topic archived. No new replies allowed.