> str` only extract the first word in C ? " />
cin Extracts Only First Word in C
In the provided code, cin's extraction of a string using cin >> str; captures only the first word, causing issues when dealing with input containing multiple words. This is due to the way cin operates in Turbo C , reading one word at a time when encountering >>.
Solution:
To extract a complete line into a character array instead of a single word, modify the cin statement to:
<code class="c++">cin.getline(str, sizeof str);</code>
Alternatively, if using a more modern C environment and working with strings, you can replace the char array with std::string and utilize getline() to read the input as follows:
<code class="c++">getline(cin, str);</code>
Additional Considerations:
The above is the detailed content of Why does `cin >> str` only extract the first word in C ?. For more information, please follow other related articles on the PHP Chinese website!