Home > Backend Development > C++ > body text

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

Susan Sarandon
Release: 2024-10-27 12:20:02
Original
319 people have browsed it

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;
Copy after login

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)
Copy after login

Even the simplest of string outputs, such as:

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

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!