Questions tagged [cout]

std::cout is the global stream object provided by the C++ standard library for writing to the standard output stream.

For more general questions about std::ostream use the or tags.

Bjarne Stroustrup, the creator of C++, explains that cout is pronounced "see-out" and the "c" stands for "character". (wcout is used for wide character output to the standard output stream)

1820 questions
15
votes
6 answers

How to rollback lines from cout?

I'm coding a task monitoring, which updates tasks' progress using cout. I'd like to display one task progress per line, therefore I have to rollback several lines of the console. I insist on "several" because \b does the job for one line, but does…
Mister Mystère
  • 952
  • 2
  • 16
  • 39
15
votes
3 answers

Using "<<" or "+" to put strings together when using "cout"

I have seen people output different strings together by using both "<<" and "+". cout << firstname << lastname << endl; versus: cout << firstname + lastname << endl; Is it better to use "<<" or does it not make much of a difference?
iDrinkJELLY
  • 153
  • 1
  • 6
15
votes
3 answers

C++ Segmentation Fault when using cout in static variable initialization

I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. When I use my own build script to build the program, it segfaults at the…
gexicide
  • 38,535
  • 21
  • 92
  • 152
15
votes
6 answers

printf more than 5 times faster than std::cout?

#include #include #include #include int main(int argc, char* argv[]) { std::clock_t start; double duration; std::cout << "Starting std::cout test." << std::endl; start = std::clock(); …
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
14
votes
1 answer

char16_t printing

Recently I had a problem with porting a Windows application to Linux because of the wchar_t size difference between these platforms. I tried to use compiler switches, but there were problems with printing those characters (I presume that GCC wcout…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
14
votes
7 answers

Assigning cout to a variable name

In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested)…
user12576
  • 378
  • 1
  • 2
  • 9
14
votes
1 answer

Why does the statement “cout << '\\\\';” not fail?

The source code is as the following. cout << '\\' << endl; //OK, output is \ cout << '\\\\' << endl; //OK, output is an integer 23644, but why? The statement cout << '\\\\' << endl; invokes the following function of class ostream. _Myt&…
Tango Xiao
  • 319
  • 3
  • 11
14
votes
2 answers

Correctly pad negative integers with zeros with std::cout

I found this question already asked, but the answer everybody gives is std::cout << std::setw(5) << std::setfill('0') << value << std::endl; which is fine for positive numbers, but with -5, it prints: 000-5 Is there a way to make it print -0005 or…
Philippe
  • 1,287
  • 11
  • 23
14
votes
1 answer

C++ reset locale to "C" globally?

In a project I am currently working on I link to a proprietary dynamic library. As soon as I run the library's initialize function, the behavior of logging and printing of numbers changes. Commas have been inserted at every third decimal. i.e. cout…
dinkelk
  • 2,676
  • 3
  • 26
  • 46
14
votes
2 answers

Why is writing a std::string to cout causing an unknown operator << error?

I am getting an error when I try to output the return value from one of my methods: Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string Main.cpp #include using namespace std; #include…
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
13
votes
6 answers

cout does no print in QtCreator

I saw this question already on this forum but I do not know why the proposed answer does not work in my case. So I try to ask for other slution. I just got my Qt creator running under Linux. I do not understand why my: cout << "This does not…
Stefano
  • 3,981
  • 8
  • 36
  • 66
13
votes
4 answers

What's the opposite of `fixed` in cout?

When using cout, what is the default formatter defined in the header? In other words, once I've set my formatter to fixed using cout << fixed << setPrecision(2), how do I change it back? Or, what am I changing it back to?
Moshe
  • 57,511
  • 78
  • 272
  • 425
13
votes
5 answers

How to make a simple C++ program in which std::cout is not flushed

In an effort to better understand buffered streams in C++, I would like to write a simple program in which the std::cout buffer is NOT flushed before termination. Since I have read that std::cout is flushed on normal termination, I tried throwing a…
ivme
  • 548
  • 5
  • 14
13
votes
2 answers

print double with precision 4 using cout

Possible Duplicate: Convert a double to fixed decimal point in C++ Suppose , I have double a = 0 and I want to print it as 0.0000 . I've tried this : cout.precision(4) ; cout<
URL87
  • 10,667
  • 35
  • 107
  • 174
12
votes
1 answer

How do you print a std::regex?

How do you print the string representation of a std::regex? Say I have a collection of patterns, and I'd like to print the first one that matches: std::vector> patterns = Get(); for (auto pattern: patterns){ if…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271