loops - Function to find number of words in a file using C++? -
while (ch != '\0') { if (iswhitespace(contents[i-1]) == 1) countwords++; i++; }
i'm trying print word count in text file. i've defined function iswhitespace, homecoming 1 if char white space, otherwise returns 0. i've tested iswhitespace function , works properly. added code word count function , i'm getting segmentation fault error. i've tried multiple methods within while loop of getting word count , they've compiled, didn't homecoming right number, it's not problem outside while loop. can explain i'm doing wrong? give thanks much!
since ch
never changes, loop doesn't end until contents[i-1]
shouldn't accessing, hence segmentation fault.
c++ loops count while-loop word
No comments:
Post a Comment