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
12
votes
8 answers

How to determine the size of an array of strings in C++?

I'm trying to simply print out the values contained in an array. I have an array of strings called 'result'. I don't know exactly how big it is because it was automatically generated. From what I've read, you can determine the size of an array by…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
12
votes
7 answers

How can I print a string to the console at specific coordinates in C++?

I'm trying to print characters in the console at specified coordinates. Up to now I have been using the very ugly printf("\033[%d;%dH%s\n", 2, 2, "str"); But I just had to ask whether C++ had any other way of doing this. The problem is not even that…
Shawn
  • 10,931
  • 18
  • 81
  • 126
12
votes
4 answers

Order of execution in operator <<

I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below A1B2 While I can see that the output I get is BA12 I thought that the call std::cout<< b->fooA() << b->fooB() <<…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
12
votes
3 answers

what is the best way to avoid negative zero in output?

As in this question is said, there is some differences between negative and positive zero in floating point numbers. I know it's because of some important reasons. what I want to know is a short code to avoid negative zero in output. for example in…
Ali
  • 6,808
  • 3
  • 37
  • 47
11
votes
1 answer

Why does overloading operator<< to print Eigen class member result in a segfault?

For the following struct struct TestClass { TestClass() : mat(Eigen::Matrix3i::Zero()) {} Eigen::Matrix3i mat; }; I would like to have an overloaded operator<< to print the mat member to std::cout. I tried std::ostream& operator<<(std::ostream&…
red
  • 195
  • 1
  • 10
11
votes
1 answer

Why is this lambda streamable?

To my surprise the following code prints 1. std::cout << [](const char* arg){ return arg[0]=='s'; } << std::endl; Can someone explain this, please?
SU3
  • 5,064
  • 3
  • 35
  • 66
11
votes
4 answers

Check if ostream object is cout or ofstream, c++

Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to…
Jordan
  • 4,928
  • 4
  • 26
  • 39
11
votes
3 answers

"cout" and "char address"

char p; cout << &p; This does not print the address of character p. It prints some characters. Why? char p; char *q; q = &p; cout << q; Even this does not. Why?
GandalfDGrey
  • 321
  • 2
  • 9
11
votes
2 answers

How to set a fixed width with cout?

I want to cout a table like output using c++. It should look like this Passes in Stock : Student Adult ------------------------------- Spadina 100 200 Bathurst 200 300 Keele 100 100 Bay …
Andrew Kim
  • 111
  • 1
  • 1
  • 3
11
votes
2 answers

Limit the precision on std::cout of default values in boost::options_description

When I construct a boost::options_description instance like options.add_options() ("double_val", value(&config.my_double)->default_value(0.2), "it's a double"); and later want to have the automated output of the options that are available for my…
Christian
  • 111
  • 3
11
votes
4 answers

C++ Unicode characters printing

I need to print some Unicode characters on the Linux terminal using iostream. Strange things happen though. When I write: cout << "\u2780"; I get: ➀, which is almost exactly what I want. However, if I write: cout << '\u2780'; I get: 14851712. The…
Sventimir
  • 1,996
  • 3
  • 14
  • 25
11
votes
6 answers

iostream thread safety, must cout and cerr be locked separately?

I understand that to avoid output intermixing access to cout and cerr by multiple threads must be synchronized. In a program that uses both cout and cerr, is it sufficient to lock them separately? or is it still unsafe to write to cout and cerr…
zaphoyd
  • 2,642
  • 1
  • 16
  • 22
11
votes
3 answers

Does std::cout have a return value?

I am curious if std::cout has a return value, because when I do this: cout << cout << ""; some hexa code is printed. What's the meaning of this printed value?
user1448323
  • 159
  • 1
  • 3
  • 7
10
votes
2 answers

Where is cout declared?

My computer science professor wants us to find the declaration of cout. I've compiled a simple Hello world program using g++ and the -E parameter. Here's what my hello.cpp looks like: #include using namespace std; int main(){ string…
Moshe
  • 57,511
  • 78
  • 272
  • 425
10
votes
1 answer

Thread safe cout technique. Am I missing something?

I'm working with some multithreaded code for a game project, and got a bit tired of sorting through the stdout vomit created by two threads using cout for debuging messages at the same time. I did some research and stared at a wall for an hour or…
Chris
  • 103
  • 1
  • 4