Home  >  Article  >  Backend Development  >  Why Can\'t I `cout` My Strings in C ?

Why Can\'t I `cout` My Strings in C ?

Susan Sarandon
Susan SarandonOriginal
2024-10-27 12:20:02320browse

Why Can't I `cout` My Strings in C  ?

The Quandary of Outputting Strings

In the realm of C , attempting to display strings using "cout" can sometimes lead to perplexing errors. Consider this puzzling scenario:

<code class="cpp">string text;
text = WordList[i].substr(0,20);
cout << "String is : " << text << endl;

Woe to the programmer who encounters this enigmatic message:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

Even the simplest of string outputs, such as:

<code class="cpp">string text;
text = "hello";
cout << "String is : " << text << endl;

seem to be met with resistance.

Enhancing Your Compilation

To conquer this predicament, two faithful companions must be summoned:

<code class="cpp">#include <string>
#include <iostream></code>

By invoking these headers, you bestow upon your code the ability to navigate the stringy waters with ease. With these comrades by your side, you can embrace the joys of "cout" once more and bid farewell to those exasperating errors.

The above is the detailed content of Why Can\'t I `cout` My Strings in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn