inputting from file stumping me

I am in the process of making a puzzle that inputs to the program comes from a file. The file contains the matrix size and word puzzle contents. The program should produce the sample output shown with one character of the border showing in the output and blank spaces between the characters.
The input from file would look like:
5 8
hptnrocr
aeehkcra
moaiauag
itmupcni
rauioibn

The max size of my array rows and columns can will be 22 and if they exceed 22 or are less than 1 than it will display an error message. Right now when I run my program nothing happens. Any ideas and help will be appreciated.. here is what I 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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <iostream>
#include <fstream>

using namespace std;

const int MAXSIZE = 22;

int main()
{
    char puzzle[MAXSIZE][MAXSIZE];
    int rows, cols;
    
    ofstream inFile;
    inFile.open ("puzzle.txt");
    cin >> rows >> cols;
    
    if (puzzle[MAXSIZE][MAXSIZE] < 1 || puzzle[MAXSIZE][MAXSIZE] > 22) {
        cout << "Row size, " << rows << ", is out of range. " 
        << "Must be 1 to " << MAXSIZE << "." << endl; 
        }              
    
    system("PAUSE");
    return EXIT_SUCCESS;
}      
puzzle[MAXSIZE][MAXSIZE] when used outside of the declaration/definition of puzzle is a non-existent element. Don't try to access elements that don't exist.

Perhaps instead of puzzle[MAXSIZE][MAXSIZE] you should use the variables you actually read into: rows and/or cols. Although, presumably you meant to read those in from inFile (from which you attempt to read nothing) and not [/i]cin[/i].
Last edited on
even if I change it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    int rows, cols;
    
    ofstream inFile;
    inFile.open ("puzzle.txt");
    cin >> rows >> cols;
    
    char puzzle[rows][cols];
    
    if (puzzle[rows][cols] < 1 || puzzle[rows][cols] > 22) {
        cout << "Row size, " << rows << ", is out of range. " 
        << "Must be 1 to " << MAXSIZE << "." << endl; 
        }              
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


I still get no output
If that code were legal, you would still be accessing nonexistent elements of the array puzzle.

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
#include<iostream>
#include <fstream>

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

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

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

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

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

    char puzzle[maxSize][maxSize];
    // extract puzzle data from inFile here.
}
Topic archived. No new replies allowed.