0

I have an input file that I would like to use by using cin. My input file contains a list of 9x9 numbers, such as:

1 2 3 4 5 6 7 8 9
2 2 3 4 5 6 7 8 9
3 2 3 4 5 6 7 8 9
4 2 3 4 5 6 7 8 9
5        ...
6        ...
7        ...
8        ...
9        ...

I want to store these values into a 2d array, so they would look like:

int board[9][9] = {{1, 2, 3, 4, 5, 6, 7, 8, 9},
    {2, 2, 3, 4, 5, 6, 7, 8, 9},
    {3, 2, 3, 4, 5, 6, 7, 8, 9},
    {4, 2, 3, 4, 5, 6, 7, 8, 9},
    {5, 2, 3, 4, 5, 6, 7, 8, 9},
    {6, 2, 3, 4, 5, 6, 7, 8, 9},
    {7, 2, 3, 4, 5, 6, 7, 8, 9},
    {8, 2, 3, 4, 5, 6, 7, 8, 9},
    {9, 2, 3, 4, 5, 6, 7, 8, 9}};

I tried to do:

int board[9][9];
for (int i=0;i<9;i++) {
    for (int j=0;j<9;j++) {
       std::cin >> board[i][j];
    }
}

However, I don't think it's working. I'm going to use them as inputs when I run my code.

5
  • 2
    What you wrote doesn't compile. for (int j=0;j++) { is invalid C++ code. Commented Aug 23, 2014 at 22:47
  • 1
    @user2899162 give it a break, it's just a typo Commented Aug 23, 2014 at 22:53
  • sorry, it was a typo. Commented Aug 23, 2014 at 23:01
  • @Vladp I was worried that was where the problem may lie; obscured by a typo. Commented Aug 23, 2014 at 23:37
  • You have to understand how "CIN" works. CIN basically expects input from a command line and takes one input at a time. However you are reading your input from a "FILE". To read from a file, you will need a data input stream that reads a file from the start of the file to the end of the file. Such input streams are ifstream inputReader(afile); or freopen("afile", "r", stdin); Commented Aug 24, 2014 at 0:00

2 Answers 2

2

This works for me in GCC 4.9.0 with C++11:

Sample Code:

#include <iostream>

int main() {
    int board[9][9];
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            std::cin >> board[i][j];
        }
    }

    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            std::cout << board[i][j];
        }
        std::cout << std::endl;
    }
    return 0;
}

You should change C array for std::vector or other container from STL, it provide a lot of benefice (automatic memory management, array bound check, etc...). If you could use C++11, the new range for loop is a big improvement too (syntactical and performance wise, it avoid error as off by one, incorrect bounds, etc...).

Here is a C++11 version:

#include <iostream>
#include <vector>

int main() {
    typedef std::vector<int> row_t;
    typedef std::vector<row_t> board_t;

    board_t board(9, row_t(9));
    for (auto& row : board) {
        for (auto& cell : row) {
            std::cin >> cell;
        }
    }

    for (const auto& row : board) {
        for (auto cell : row) {
            std::cout << cell;
        }
        std::cout << std::endl;
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

One might also consider a typedef std::vector<int> row_t to make the first vector declaration more readable.
Yes it's better that way, in this explicit case it's a little overkill, but is the correct way, updating code.
0

The inner loop is wrong, there you have j++ as the loop condition. And as in the first iteration j will be zero (which in C++ is the same as false) the loop will not iterate at all. Besides, the inner loop is missing a semicolon, so it shouldn't even compile.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.