0

whats the prob

#include <iostream>
using namespace std;
int main() {
    int M; //height
    int N; //length
    cin >> M;
    cin >> N;
    int list[M][N], smaller[N];
    string smaller_str;
    for (int i = 0;i < M; ++i){
        getline(cin, smaller_str);
        for (int j = 0; j < N; i = i++) {
            cin >> smaller_str[j];
        }
        list[i] = smaller;
    }
}

i want to put 1D array "smaller" in to 2D array list however i do get errors in the "list[i] = smaller;" part i need help guys

secret
  • 3
  • 1

2 Answers2

1

Unless you want to check for errors, you won't need to use getline in this case. Just read each elements via >> operator.

Also note that Variable-Length Arrays like int list[M][N]; is not in the standard C++. You should use std::vector instead of that.

Another point is the the i = i++ in the inner loop is wrong. It should be ++j.

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int M; //height
    int N; //length
    cin >> M;
    cin >> N;
    //int list[M][N];
    std::vector<std::vector<int> > list(M, std::vector<int>(N));
    for (int i = 0;i < M; ++i){
        for (int j = 0; j < N; ++j) {
            cin >> list[i][j];
        }
    }
}
MikeCAT
  • 61,086
  • 10
  • 41
  • 58
0

You cannot assign another arrays to an array, what you can do is copy/ move its content. For this, use std::copy,

...
int list[M][N], smaller[N];
string smaller_str;
for (int i = 0; i < M; ++i) {
    getline(cin, smaller_str);
    for (int j = 0; j < N; i = i++) {
        cin >> smaller_str[j];
    }

    std::copy(smaller, smaller + N, reinterpret_cast<int*>(&list[i]));
}

Note: If you would like to move the content rather than copying, swap the std::copy with std::move.

D-RAJ
  • 2,907
  • 1
  • 4
  • 20