for loop skips one round on reading values to char array in c++ -
so got weird thing that's happening me. have 2 dimensional char array, i'm suppose put values in array following conditions:
- each word (string) should in different array (different line)
- "." indicator end of input.
this code:
const int max_strs = 10, max_str_len = 8; int main() { char dict[max_strs][max_str_len] = { 0 }; getarray(dict); return exit_success; } void getarray(char array[][max_str_len]) { bool dot = false; int i, j; ( = 0; < max_strs && !dot; i++) ( j = 0; j < max_str_len && !isspace(array[i][j - 1]); j++) { array[i][j] = cin.get(); if (array[i][j] == '.') { dot = true; break; } } }
the line i'm trying read is:
blabla picked nice peter hahaha of pickled piper .
for reason every time i=8, j automatically has same value (j=8) , skips round of loop.
help appreciated!
the problem when j=0, asked him check if array[-1] has whitespace.
anyway here fixed code:
void getarray(char array[][max_str_len]) { bool dot = false; int i, j; ( = 0; < max_strs && !dot; i++) ( j = 0; j < max_str_len ; j++) { array[i][j] = cin.get(); if (array[i][j] == '.') { dot = true; break; } if( isspace(array[i][j])) break; } }
thank alan stokes bringing attention!
Comments
Post a Comment