Turbo C 's "cin" Limitation: Reading Only the First Word
In Turbo C , the "cin" input operator has a limitation when dealing with character arrays. Specifically, it only reads until it encounters a whitespace character (e.g., space or newline). This can lead to unexpected behavior when trying to read multi-word input.
Consider the following Turbo C code:
<code class="c++">#include <iostream.h> class String { char str[100]; public: void input() { cout << "Enter string: "; cin >> str; } void display() { cout << str; } }; int main() { String s; s.input(); s.display(); }</code>
If you run this code and enter the input "Steve Hawking," you would expect the output to display the entire string. However, due to the "cin" limitation, only "Steve" is displayed, because "cin" stops reading at the first whitespace character (space).
Overcoming the Limitation
To address this limitation, you can use alternative methods for reading character arrays in Turbo C :
Recommendation
The recommended approach nowadays is to use modern C compilers and the standard library. This provides more reliable and efficient input handling, including the ability to read entire lines of input.
The above is the detailed content of Why Does Turbo C \'s \'cin\' Only Read the First Word?. For more information, please follow other related articles on the PHP Chinese website!