c++ - Error in inputting a 2d character array -
i entering 2d character array , have stop entering when user hit enter key . code not showing output.
input:
5 // number of columns, // number of rows unknown have taken maximum rows as: 40 array:
toioynnkpheleaigshareconhtomesnlewx expected output:
i = 7, j = 5 here code:
int main(){ char a[100][100]; int n, i, j, p, q; cin >> n; if(n==0) exit(0); for(i = 0; < 40; i++){ for(j = 0; j < n; j++){ cin >> a[i][j]; if(a[i][j]==13) // 13 = ascii code enter key goto jump; } } jump: cout<<i<<"\n"<<j<<"\n"; } but not printing anything.
what wrong it?
this happening because cin ignores white space , newline ('\n', ascii code 13). means condition if(a[i][j] == 13) never evaluates true.
solution: use cin.get(a[i][j]) instead of cin>>a[i][j]
this works because cin.get() method not ignore newline character ('\n').
Comments
Post a Comment