scanning a 2D char Array in C++ -
i trying scan 2d char array in c++ program. problem code not scan entire array expected. ex if want scan 41*41 array reason stops @ 40th row , when press enter scans 1 remaining row. here simple piece of code.
#include <iostream> char g[101][101]; int n,m; using namespace std; int main(int argc, const char * argv[]) { cin >> m >> n; cout << "m n scanned" << m << n << "\n"; (int t =0;t<m;t++) { (int j = 0;j<n;j++) { cin >> g[t][j]; cout << "scanned " << t << " " << j << "\n"; } } return 0; }
compiling: testproj $g++ main.cpp -o main
what missing here?
edit: input 41 41 <2d char array of 41*41>
expected output: scanned 40 40
actual output scanned 39 40
press enter
....
scanned 40 40
works me.
input file, gets piped executed code on standard input:
41 41 ***************************************** ***************************************** [ 39 more rows of same ]
output:
m n scanned4141 scanned 0 0 scanned 0 1 scanned 0 2 ... scanned 40 36 scanned 40 37 scanned 40 38 scanned 40 39 scanned 40 40
there's nothing wrong shown code, when used is.
i suspect forgot little detail operator>>
on char
values going skip whitespace, in addition newline characters, , input matrix contains spaces, ignored, , code continues 41*41 non-space characters read. so, @ point, code waiting read expected amount of unread non-whitespace input.
Comments
Post a Comment