-2

I'm working on a program to read input in from a file in c++. Each line starts with a number to determine the size of a two dimensional array. It is followed by a couple numbers to determine which row and column to change from a 0 to a 1, and finally print the array. However whenever I try to run the program I just end up with "segmentation fault(core dumped)." Any help with what I might be doing wrong here would be appreciated.

#include <stack>
#include <stdexcept>
#include <fstream>
#include <array>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <string>

using namespace std;

int main(int argc, char** argv)
{
    if (argc < 3)
    {
        throw invalid_argument("Usage: ./hello <INPUT FILE> <OUTPUT FILE>");
    }

    ifstream input;
    ofstream output;

    input.open(argv[1]);
    output.open(argv[2]);

    string line;

    char* com, *op;

    while(getline(input, line))
    {
        com = strdup(line.c_str());
        op = strtok(com, "\t");

        int n = stoi(op);
        int board[n][n]={{0}};
    
        
        istringstream iss(line);
        int a, b;
        board[a][b] = 1;
        if (!(iss >> a >> b))
        {
            break;
        }
        
        for (int i = 0; i<n; i++)
        {
            for (int j = 0; j<n; j++)
                cout << " " << board[i][j] << " ";
            printf("\n");
        }

    }
    input.close();
    output.close();
    return 0;
}```
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Satoly
  • 1
  • 1
    First mistake is that `int board[n][n]={{0}};` is not legal C++. Use vectors instead. – john Oct 25 '22 at 05:15
  • Second mistake is that you have `board[a][b] = 1;` before `iss >> a >> b` not afterwards. – john Oct 25 '22 at 05:18
  • Third mistake is that `strdup` leaks memory – john Oct 25 '22 at 05:18
  • Fourth mistake is that using `strtok` modifies the `line` variable, so the subsequent read will fail. Since you are using an `istringstream`. just read the array size from that instead of using strtok and strdup and stoi. `iss >> n;` Simple. – john Oct 25 '22 at 05:19
  • For strings use std::string, avoid "C" style functions like `strdup` and `strok` . Do you know your board size up front then use `std::array,height>` (with width and height constants) otherwise use `std::vector>` (and make sure you set the vectors to the desired sizes at runtime). Oh and stop using : `using namespace std;` learn to type `std::` where needed. – Pepijn Kramer Oct 25 '22 at 05:23
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 25 '22 at 05:56
  • I would recommend to avoid "2d memory" there is nothing like that physically. Use dynamic contigous memory for example std::vector board(n * n,0); This creates a vector named board of size n* n with value 0. To access your "2d" you can use board.at(a * n+b). board[n][n] is a C11 construct called variable length array which is not standard C++ as already mentioned. – akira hinoshiro Oct 25 '22 at 06:32
  • (It seems that, the array memory don't be needed actually, and what required is just output 1 at only one position in the output process.) – fana Oct 25 '22 at 06:53

1 Answers1

-2

Here's the code modified as described in the comments above

    istringstream iss(line);
    iss >> n;

    vector<vector<int>> board(n, vector<int>(n));
    
    int a, b;
    if (!(iss >> a >> b))
    {
        break;
    }
    board[a][b] = 1;
    
    for (int i = 0; i<n; i++)
    {
        for (int j = 0; j<n; j++)
            cout << " " << board[i][j] << " ";
        printf("\n");
    }

Untested code.

john
  • 85,011
  • 4
  • 57
  • 81