Random print program, prints multiple outputs at once despite sleep() function? [duplicate] - c++

This question already has answers here:
std::cout won't print
(4 answers)
Closed 9 years ago.
So, I have the program below. What it does is, it prints random numbers while formating them with a width of 10 on the console.
Now, when I added the sleep function, I expected it to print one number every 10 milliseconds (or more), but what it does is, it prints 100 outputs every 100*10 milliseconds. I was wondering, why does this happen? Does the output get buffered or what?
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
int main()
{
srand(time(0));
for (int i = 1; i < 1000000; i++)
{
usleep(10*1000); // No matter the input, the same thing happens
cout << setw(10) << rand()%(90*i) + 10*i;
}
}
I tried this both on windows and unix and it's the exact same thing.

Yes, the output does get buffered.
Use std::cout.flush(); to flush manually.
As a note, std::endl (appends a new line and) does the flush.

Related

c++ stdout flushes stream unexpectedly

#include <iostream>
#include <thread>
using namespace std;
int main()
{
for (int i = 0; i < 10000; i++) {
cout << "Hello\n";
}
this_thread::sleep_for(chrono::milliseconds(2000));
cout << "2 seconds have passed" << endl;
return 0;
}
In my code, I didn't call any std::flush or std::endl, but the hello's are printed before the 2 seconds delay. I am expecting to print all the hello's after the 2 seconds delay, but it didn't. My code runs like this:
Hello
Hello
.
.
.
Hello
Hello
(after 2 seconds)
2 seconds have passed
[terminated]
Why is this happening?
First of all, you're writing more output than a typical file buffer will hold, so you'd almost always expect at least some of the output to show up before the sleep.
Second, you're doing a lot of separate output calls, so if cout is unit-buffered, each one is going to be flushed immediately.
Third, you're writing a new-line at the end of each item, so if cout is line-buffered, (yup) each one is going to be flushed immediately.
So, if you want a better chance of seeing at least some of the output showing up after the sleep ends, turn off unit buffering and get rid of the new-lines:
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
cout << nounitbuf;
for (int i = 0; i < 1000; i++) {
cout << "Hello";
}
// display something different to make it easier for user to see
// whether all output showed up before sleep or not.
cout << "...";
this_thread::sleep_for(chrono::milliseconds(2000));
cout << "2 seconds have passed" << endl;
return 0;
}
But even with this, there's no guarantee the behavior will change. Rather the contrary, most implementations go to some pain to assure that output written to the console shows up as promptly as possible, so even when you take steps toward delaying it, it'll still probably show up before the sleep. But this might improve your chances a little bit anyway.

Why does the following program give different output in C++?

Following program prints 1 2 3 4 5 at once. That means there is no time delay in printing the output.
#include<iostream>
#include<stdio.h>
#include <thread>
using namespace std;
int main()
{
for(int i = 1; i <= 5; ++i)
{
cout << i << " ";
// Function to sleep the thread
this_thread::sleep_for(500ms);
}
return 0;
}
But this program prints
1
2
3
4
5
one by one that means I’m getting the output with 0.5 sec time delay.
#include<iostream>
#include<stdio.h>
#include <thread>
using namespace std;
int main()
{
for(int i = 1; i <= 5; ++i)
{
cout << i << "\n";
// Function to sleep thread
// for 0.5 sec
this_thread::sleep_for(500ms);
}
return 0;
}
What is literally happening in both of above programs?
Note:You can't see the difference of both the outputs in online compiler because they show the result after termination of program.
The output may be line buffered, in which case only complete lines are sent to the underlying output device.
I'm able to get the desired output, one element at a time appears with 0.5 sec of delay. Used visual studio 2015.
I suggest you to avoid online compilers.
They generally give the complete output once in a text box.
"\n" makes the output go to next line. It is newline character

How to ask for input after text to show the next line in C++? [duplicate]

This question already has answers here:
Wait until user presses enter in C++?
(3 answers)
Closed 7 years ago.
I am a total newbie to C++ and I need to have 3 line of text and after every line I have to ask the user to press enter to continue. How can I do it?
Here is the code that I have so far:
#include <iostream>
using namespace std;
int main()
{
std::cout << "Es esmu dators.";
std::cout << "Es zinu C++.";
std::cout << "C++ ir programmesanas valoda";
}
You can use getchar() after each line. To use getchar() you must include cstdio.
Example code:
#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
std::cout << "Es esmu dators.\n";
getchar();
std::cout << "Es zinu C++.\n";
getchar();
std::cout << "C++ ir programmesanas valoda\n";
}
From this answer:
Several ways to do so, here are some possible one-line approaches:
Use getch() (need #include ).
Use getchar() (expected for Enter, need #include ).
Use cin.get() (expected for Enter, need #include ).
Use system("pause") (need #include ).
PS: This method will also print Press any key to continue . . . on the screen. (seems perfect choice for you :))
You should also perform a quick search on the site to see if your question has been asked before, going forward.

How to output Polish characters in C++ console application? [duplicate]

This question already has answers here:
How I can print the wchar_t values to console?
(8 answers)
Closed 9 years ago.
I've try this simple code to output polish characters using 'std::wstring' class. The class is constructed succesfully from wchar_t array but I don't know how to output it to the screen. That line "cout << X << endl ;" doesn't compile. Is it possible to output polish characters in console application written in native C++ ?. If so then how to work around this ?. Below is a simple code I did try to compile:
#include <iostream>
#include <conio.h>
#include <string>
int main(void)
{
using namespace std ;
const wchar_t data[] = {'ą', 'ę', 'ć'} ;
wstring X(data) ;
cout << X << endl ;
getch() ;
return 0 ;
}
Use std::wcout instead of cout
After using wcout you should no longer use cout in your program. The first time you cout or wcout it sets the orientation of stdout for the duration of your program.

C++ Floating point Display [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
floating point precision
when I do cout<<8.0 .Its getting printed as 8.How to Print in the output console of c++ the entire zeros after decimal point like 8.00000000
I tried this cout<<setprecision(5)<<(double)8.0; still printing 8
Use the fixed manipulator
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(6) << (double)8 << "\n";
return 0;
}
http://ideone.com/ShcNIc
See How do I print a double value with full precision using cout?
cout.precision(15);
cout << fixed << 8.0;