Reputation: 3
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
Upvotes: 0
Views: 61
Reputation: 75062
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];
}
}
}
Upvotes: 1
Reputation: 3380
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
.
Upvotes: 0