Questions tagged [cout]
std::cout is the global stream object provided by the C++ standard library for writing to the standard output stream.
1,758
questions
459votes
16answers
426kviews
'printf' vs. 'cout' in C++
What is the difference between printf() and cout in C++?
407votes
16answers
583kviews
How do I print a double value with full precision using cout?
In my earlier question I was printing a double using cout that got rounded when I wasn't expecting it. How can I make cout print a double using full precision?
360votes
29answers
966kviews
How do I print out the contents of a vector?
How do I print out the contents of a std::vector to the screen?
A solution that implements the following operator<< would be nice as well:
template<container C, class T, String delim = "...
297votes
7answers
282kviews
How can I pad an int with leading zeros when using cout << operator? [duplicate]
I want cout to output an int with leading zeros, so the value 1 would be printed as 001 and the value 25 printed as 025. How can I do this?
277votes
5answers
289kviews
undefined reference to 'std::cout'
Shall this be the example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
It throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In ...
238votes
3answers
356kviews
cout is not a member of std
I'm practicing using mulitple files and header files etc. So I have this project which takes two numbers and then adds them. Pretty simple.
Here are my files:
main.cpp
#include <iostream>
#...
188votes
8answers
112kviews
uint8_t can't be printed with cout
I have a weird problem about working with integers in C++.
I wrote a simple program that sets a value to a variable and then prints it, but it is not working as expected.
My program has only two ...
180votes
11answers
324kviews
How to print to console when using Qt
I'm using Qt4 and C++ for making some programs in computer graphics. I need to be able to print some variables in my console at run-time, not debugging, but cout doesn't seem to work even if I add the ...
176votes
10answers
299kviews
C++ cout hex values?
I want to do:
int a = 255;
cout << a;
and have it show FF in the output, how would I do this?
151votes
7answers
246kviews
Why I cannot cout a string?
Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 ...
116votes
7answers
79kviews
What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?
I tried researching the difference between cout, cerr and clog on the internet but couldn't find a perfect answer. I still am not clear on when to use which. Can anyone explain to me, through simple ...
90votes
8answers
112kviews
How to make C++ cout not use scientific notation
double x = 1500;
for(int k = 0; k<10 ; k++){
double t = 0;
for(int i=0; i<12; i++){
t += x * 0.0675;
x += x * 0.0675;
}
cout<<"Bas ana: "<<x&...
81votes
2answers
451kviews
'cout' was not declared in this scope [closed]
I have a C++ program:
test.cpp
#include<iostream>
int main()
{
char t = 'f';
char *t1;
char **t2;
cout<<t; //this causes an error, cout was not declared in this scope
...
79votes
17answers
150kviews
how do I print an unsigned char as hex in c++ using ostream?
I want to work with unsigned 8-bit variables in C++. Either unsigned char or uint8_t do the trick as far as the arithmetic is concerned (which is expected, since AFAIK uint8_t is just an alias for ...
76votes
11answers
196kviews
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 ...
75votes
2answers
161kviews
C++ printing boolean, what is displayed?
I print a bool to an output stream like this:
#include <iostream>
int main()
{
std::cout << false << std::endl;
}
Does the standard require a specific result on the stream (e....
69votes
8answers
131kviews
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 ...
64votes
8answers
29kviews
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 :" <&...
63votes
5answers
5kviews
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.
55votes
1answer
24kviews
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 ...
50votes
9answers
20kviews
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 ...
49votes
8answers
74kviews
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 ...
48votes
2answers
47kviews
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 ...
42votes
3answers
59kviews
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 ...
41votes
4answers
64kviews
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::...
38votes
4answers
55kviews
How to remove last character put to std::cout?
Is it possible on Windows without using WinAPI?
34votes
15answers
31kviews
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 ...
34votes
4answers
87kviews
std::cout won't print
Is there any circumstance when std::cout << "hello" doesn't work? I have a c/c++ code, however the std::cout doesn't print anything, not even constant strings (such as "hello").
Is there any ...
31votes
2answers
90kviews
How to cout a float number with n decimal places [duplicate]
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 ...
31votes
3answers
2kviews
Why is '\n' preferred over "\n" for output streams?
In this answer we can read that:
I suppose there's little difference between using '\n' or using "\n", but the latter is an array of (two) characters, which has to be printed character by ...
31votes
2answers
923views
Type of ternary expression
Can anyone explain the output of the following program:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "First character " << '1' << endl;
...
29votes
5answers
91kviews
How do I correctly organize output into columns?
The first thing that comes to my mind is to do a bunch of \t's, but that would cause words to be misaligned if any word is longer than any other word by a few characters.
For example, I would like to ...
28votes
3answers
213kviews
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 ...
28votes
3answers
4kviews
cout << order of call to functions it prints?
the following code:
myQueue.enqueue('a');
myQueue.enqueue('b');
cout << myQueue.dequeue() << myQueue.dequeue();
prints "ba" to the console
while:
myQueue.enqueue('a');
myQueue.enqueue('...
27votes
5answers
2kviews
How does C++ interpret a cout with a '+' in it?
I've been moving back and forth with Java/C++ so I messed up with my console output and accidentally wrote lines like:
cout << "num" + numSamples << endl;
cout << "max" + ...
26votes
9answers
3kviews
Why does this output of the same expression from printf differ from cout?
I'm using Visual C++ 2012 and compiling from the command line the following files:
#include <stdio.h>
int main()
{
printf("%.5f", 18/4+18%4);
return 0;
}
Linking with MSVCRT.LIB ...
24votes
5answers
76kviews
cout not printing unsigned char
I am working on below code:
#include<iostream>
#include<stdio.h>
using namespace std;
main() {
unsigned char a;
a=1;
printf("%d", a);
cout<<a;
}
It is printing 1 ...
24votes
3answers
2kviews
Why is the address of this volatile variable always at 1?
I wanted to inspect the address of my variable
volatile int clock;
cout << &clock;
But it always says that x is at address 1. Am i doing something wrong??
23votes
7answers
98kviews
Float formatting in C++
How do you format a float in C++ to output to two decimal places rounded up? I'm having no luck with setw and setprecision as my compiler just tells me they are not defined.
cout << "Total :...
23votes
5answers
1kviews
Insight into how things get printed onto the screen (cout,printf) and origin of really complex stuff that I can't seem to find on textbooks
I've always wondered this, and still haven't found the answer. Whenever we use "cout" or "printf" how exactly is that printed on the screen?. How does the text come out as it does...(probably quite a ...
23votes
4answers
24kviews
How to get console output in Visual Studio 2012 Unit Tests
I have a managed C++ unit test in VS 2012. The test runs fine and I can
verify that a loop with multiple cout calls is executed.
However when I look at the test explorer the test is marked as passed
...
21votes
6answers
23kviews
How to disable cout output in the runtime?
I often use cout for debugging purpose in many different places in my code, and then I get frustrated and comment all of them manually.
Is there a way to suppress cout output in the runtime?
And ...
21votes
5answers
10kviews
Is std::cout buffered?
Just reading an old but interesting article by "Scott Meyers"
http://aristeia.com/Papers/C++ReportColumns/novdec95.pdf
Basically it is about preferring to use '\n' over std::endl (which I agree ...
21votes
2answers
12kviews
What is the difference between std::cout and std::wcout?
In c++ what is the difference between std::cout and std::wcout?
They both control output to a stream buffer or print stuff to the console, or are they just alike ?
20votes
2answers
7kviews
std::cout equivalent at compile time, or static_assert stringification of compile-time constant values in c++11
Is there a way to print the value of a constexpr or #defined value at compile time? I want the equivalent of std::cout <<, or some way to do something like
constexpr int PI_INT = 4;
...
19votes
6answers
18kviews
How do I use for_each to output to cout?
Is there a more straight-forward way to do this?
for_each(v_Numbers.begin(), v_Numbers.end(), bind1st(operator<<, cout));
Without an explicit for loop, if possible.
EDIT:
How to do this for ...
19votes
3answers
41kviews
Hide user input on password prompt [duplicate]
Possible Duplicate:
Read a password from std::cin
I don't work normally with the console, so my question is maybe very easy to answer or impossible to do .
Is it possible to "decouple" cin and ...
19votes
4answers
20kviews
Why is std::cout not printing the correct value for my int8_t number?
I have something like:
int8_t value;
value = -27;
std::cout << value << std::endl;
When I run my program I get a wrong random value of <E5> outputted to the screen, but when I run ...
18votes
6answers
43kviews
Very simple program not working in c++?
I can't figure out why this isn't working...
I am working in Linux
g++ doesn't do anything
gcc prints the following:
/tmp/ccyg7NDd.o: In function `main':
test.cc:(.text+0x14): undefined reference ...
18votes
3answers
14kviews
Called a function with "cout" statement inside a "cout" statement
I came across this rather vague behavior when messing around with code , here's the example :
#include <iostream>
using namespace std;
int print(void);
int main(void)
{
cout << "...