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

https://code.sololearn.com/ct74dvxurxWz/?ref=app

22nd Aug 2022, 10:42 PM
Malachite
Malachite - avatar
4 Respostas
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.
22nd Aug 2022, 10:51 PM
Shadow
Shadow - avatar
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
23rd Aug 2022, 2:04 AM
Malachite
Malachite - avatar
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.
23rd Aug 2022, 3:19 AM
lion
lion - avatar
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
23rd Aug 2022, 3:45 AM
Malachite
Malachite - avatar