0
Why does my code only count one word in string even if I type word multiple times and it only counts first word in str
4 ответов
0
The issue is likely that std::cin stops extracting input when encountering a space. Consider using fgets() or std::string in combination with std::getline() instead.
0
Thank you. After looking into it I'm certain you're correct. Although I can't get getline to work with my code. Keep getting errors
0
getline() needs a string, not a char array or pointer.
But then your function uses a char pointer, so you have to change that to use a string.
But then you have a member variable named string, which will cause an error. Since you don't use any of your member variables except count, you could delete them, and then modify your function accordingly to work with string instead of char*.
Other than that, your function seems to work fine, it counts the words correctly.
0
Thank you both!!! I used a temp string and used
const char *pointer = temp.c_str() to temp convert string to usable char. Good for a single use solution but I'll know for next time to do my whole code from scratch with getline() in mind