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
82
votes
10 answers

How to print Unicode character in C++

I am trying to print a Russian "ф" (U+0444 CYRILLIC SMALL LETTER EF) character, which is given a code of decimal 1092. Using C++, how can I print out this character? I would have thought something along the lines of the following would work,…
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
74
votes
7 answers

C++ alignment when printing cout <<

Is there a way to align text when printing using std::cout? I'm using tabs, but when the words are too big they won't be aligned anymore. Sales Report for September 15, 2010 Artist Title Price Genre Disc Sale Tax Cash Merle Blue …
user69514
  • 26,935
  • 59
  • 154
  • 188
69
votes
8 answers

Why is address of char data not displayed?

class Address { int i ; char b; string c; public: void showMap ( void ) ; }; void Address :: showMap ( void ) { cout << "address of int :" << &i << endl ; cout << "address of char :"…
user478571
64
votes
5 answers

what does cout << "\n"[a==N]; do?

In the following example: cout<<"\n"[a==N]; I have no clue about what the [] option does in cout, but it does not print a newline when the value of a is equal to N.
Siva Prasad
  • 753
  • 5
  • 10
58
votes
1 answer

how does cout << actually work?

I was wondering how std::cout is able to use << as it does. My main puzzlement is with whether std::cout as an instance of something. Basically, how is << defined? If I do this for a custom class, I need an instance of some sort... I could see…
Ori
  • 4,961
  • 10
  • 40
  • 39
57
votes
2 answers

c++ force std::cout flush (print to screen)

I have code such as the following: std::cout << "Beginning computations..."; // output 1 computations(); std::cout << " done!\n"; // output 2 The problem, however, is that often output #1 and output #2 appear (virtually)…
synaptik
  • 8,971
  • 16
  • 71
  • 98
54
votes
8 answers

Floating point format for std::ostream

How do I do the following with std::cout? double my_double = 42.0; char str[12]; printf_s("%11.6lf", my_double); // Prints " 42.000000" I am just about ready to give up and use sprintf_s. More generally, where can I find a reference on std::ostream…
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
52
votes
9 answers

mixing cout and printf for faster output

After performing some tests I noticed that printf is much faster than cout. I know that it's implementation dependent, but on my Linux box printf is 8x faster. So my idea is to mix the two printing methods: I want to use cout for simple prints, and…
Jabba
  • 19,598
  • 6
  • 52
  • 45
50
votes
4 answers

getting cout output to a std::string

I have the following cout statement. I use char arrays because I have to pass to vsnprintf to convert variable argument list and store in Msg. Is there any way we can get cout output to C++ std::string? char Msg[100]; char appname1[100]; char…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
44
votes
4 answers

std::cout won't print

Is there any circumstance when std::cout << "hello" doesn't work? I have a C++ program where std::cout doesn't seem to print anything, not even constant strings (such as "hello"). Is there any way to check if cout is able/unable to open the stream?…
mahmood
  • 23,197
  • 49
  • 147
  • 242
43
votes
3 answers

std::cout to print character N times

How can I print a character N number of times using std::cout without looping? Is there a way to move the text cursor back to nullify the effect of std::cout << std::endl;? i.e. to move up a line (say we never printed anything after doing the…
shiraz
  • 1,208
  • 2
  • 12
  • 26
39
votes
4 answers

How to remove last character put to std::cout?

Is it possible on Windows without using WinAPI?
Xirdus
  • 2,997
  • 6
  • 28
  • 36
37
votes
15 answers

cout or printf which of the two has a faster execution speed C++?

I have been coding in C++ for a long time. I always wondered which has a faster execution speed printf or cout? Situation: I am designing an application in C++ and I have certain constraints such as time limit for execution. My application has loads…
pirate
  • 1,583
  • 3
  • 14
  • 12
35
votes
2 answers

How to cout a float number with n decimal places

Possible Duplicate: How do I print a double value with full precision using cout? float a = 175.; cout << a; If I run the previous code I'll get just 175, how can I cout the number with (for example) 3 decimal places even they were zeros ..…
Muhammad Barrima
  • 565
  • 3
  • 6
  • 9
34
votes
4 answers

how to print a string to console in c++

Im trying to print a string to console in c++ console application. void Divisibility::print(int number, bool divisible) { if(divisible == true) { cout << number << " is divisible by" << divisibleBy << endl; } else { …
AngryDuck
  • 4,358
  • 13
  • 57
  • 91