how do i get my program to print the text from file

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. How do i get my program to read from the file and print it on the screen? How would I integrate this into the code I already have?

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;
     }
}
Either your file is missing from the desired location (Try using absolute path of the file) or there might be some other error. You are not able to see them because you are using return 0; after every if { } block.
I've changed my code to this...

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
#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";
    }

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

    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";
    }

    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";
    }

    char puzzle[maxSize][maxSize];
    
    printPuzzle(puzzle);
    
    cin.get();
    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;
     }
}


Now the output is just a bunch of random gibberish. Almost like random code that you cant read. Any ideas?
You said your input file would be :

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



Does that make sense ?
Or is there any specific way in which you want the text formatted ?
The text will be in the format in which I said. It would be inputted from a text from
Topic archived. No new replies allowed.