Only display output for a given time - c++

I just started learning c++ and am using codeblocks. I'm just wondering if there was a way to set my output to display for only a certain amount of time.
For example, I have the code
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cout << "You need to learn C++!" << endl;
return 0;
}
Now when I hit build and run, it displays the code and says "Press any key to continue". I want to know if there is a way I can set a timer for that display to go away instead of pressing a key.
Thank you!

Closing the window is not the responsibility of the program to do, it's the user responsibility. Anyway, you could stop the program for the time you want using for stopping the program and for measuring the time you want it to stop. If you double click the .exe file generated after compilation it will automatically close after execution, otherwise you will have to close the window by yourself.
More information here, about the std::thread method: http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
And here about the chrono library:https://en.cppreference.com/w/cpp/chrono/duration
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main(){
cout << "Hello world!" << endl;
cout << "You need to learn C++!" << endl;
this_thread::sleep_for (chrono::seconds(3));
cout<<"Program finished!";
return 0;
}
You could also use chrono::miliseconds or chrono::minutes or chrono::hours
There may also be a platform dependent library for closing the window . You could have one for windows, another for linux, but anyway you shouldn't be worrying about that as a beginner.

Related

Pause For-Loop for 1 second in C++

I have an loop, but it goes to fast. I need something simple and easy to use, to pause it for 1 sec in each loop.
for(int i=0;i<=500;i++){
cout << "Hello number : " << i;
//i need here something like a pause for 1 sec
}
std::this_thread::sleep_for is exactly what you're looking for.
for(int i=0;i<=500;i++){
cout << "Hello number : " << i;
std::this_thread::sleep_for(1s);
}
To use it like that, you need to include <chrono> and <thread> and then add using namespace std::chrono_literals;. It also requires c++11 enabled.
Sleep(n) is a prepared method. To use this method do not forget to add "windows.h" header file as well and remember 'n' is the millisecond you may wish to delay the code execution. A simple code which repeats "Hello world!" can be seen:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
for(int i=0;i<10;i++)
{
cout << "Hello world!" << endl;
Sleep(1000);
}
return 0;
}
If you use windows platform this may help:
#include <windows.h> //winapi header
Sleep(1000);//function to make app to pause for a second and continue after that

sleep statement not working properly in C++

I have been using the sleep statement like this: sleep(2); for a while but now it suddenly doesn't work properly anymore. Whenever I run this code (example):
#include <iostream>
using namespace std;
int main() {
cout << "Hi";
sleep(2);
cout << "Hello";
}
instead of saying "Hi" first then waiting two seconds and then saying "Hello", it first waits two seconds and then it displays both "Hi" and "Hello". I have other pieces of code that I wrote before and they do not have the problem, but as soon as I create a new target && file and try to write some code with the sleep statement in it, it does the same thing again, :(.
Please help me fix this guys, thanks!
Try flushing the buffer.
#include <iostream>
using namespace std;
int main() {
cout << "Hi";
cout << flush; // add this line
sleep(2);
cout << "Hello";
}
According to std reference, you should use:
std::this_thread::sleep_for(2s);
Of course, if you are using multi-thread environment this is best practise.

What to do to see the output of "cout" command?

I am starting with C++ (Visual Studio 2015 and Windows 8.1), with this simple code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
return 0;
}
But, the output screen shows nothing!, what shall I do?
Thanks in advance.
In Visual Studio, start the program with Ctrl-F5 and it will run and pause automagically for you. No additional code needed.
Your code is perfectly fine but the program currently only prints and exits right after, because this can happen very fast you might not be able to even see it,try pausing it :
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
cin.get();
return 0;
}
Also, make sure your Anti Virus isn't blocking Visual Studio.
Your code is just fine, however, if you execute it as a cmd program, the program window will close immediately, you might not be able to even see the output. You can write extra code to solve this problem by "pausing" the program:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "Hello world" << endl;
system("PAUSE");
return 0;
}
if you don't like include a windows.h file every time you type, you can add a "cin.get();" in the end of the code. But to be honest, since you are just a beginner, the coolest way I think you should try, is not to use Visual Studio to learn C/C++ but to install CodeBlocks(a simple but effective IDE) to write some codes that are not so long. You know, VS is for huge and complex projects and some practical program developing.
Another solution, platform dependent. My answer is for those of you who just need test pause for debugging purposes. It's not recommended release solution!
windows
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("pause");
return 0;
}
linux (and many alternatives)
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("read -rsp $'Press enter to continue...\n'");
return 0;
}
Detecting paltform
I used to do this on programming homework assignments, ensuring this only happens on windows:
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
#ifdef _WIN32
system("pause");
return 0;
}
Here's a good cheatsheet for ifdef macros and operating systems: http://sourceforge.net/p/predef/wiki/OperatingSystems/
The program exits on return 0; and window closes. Before this, you must pause the program. E.g you can wait for an input.
Here is a snippet from my code to do this. It works in both windows and linux.
#include <iostream>
using std::cout;
using std::cin;
// Clear and pause methods
#ifdef _WIN32
// For windows
void waitForAnyKey() {
system("pause");
}
#elif __linux__
// For linux
void waitForAnyKey() {
cout << "Press any key to continue...";
system("read -s -N 1"); // Continues when pressed a key like windows
}
#endif
int main() {
cout << "Hello World!\n";
waitForAnyKey();
return 0;
}

Dev-C++ Hello world doesn't show

I am new to C++. I downloaded and run Dev-C++ and I write and run F9 this:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
return 0;
}
But no "Hello, world!" is printed, why?
Many IDE users have this problem. The program runs but it closes before you can see its results on the screen. One portable fix is to add this at the bottom of main before you return:
std::cin.get();
That way it will wait for you to enter some text before it exits.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
getchar();
return 0;
}
Add getchar() at the end of your program as a simple "pause-method" as consoles seems to close so fast, so you need to "delay" to see your console.
The output is printed to a terminal, and you don't have a newline etc.... very unlikely that you will see it, so
Add a newline to the output
make sure you have time to read the output before the terminal window closes (add a sleep or something)
Don't use using namespace as that is a bad practice and will lead to trouble in your programming.
So like;
#include <iostream>
#include <unistd.h>
int main()
{
std::cout << "Hello, world!" << std::endl;
sleep(2);
return 0;
}

Run two executables with system()

I'm trying to write a small program that just runs two executables. Currently it only runs the first one for some reason:
#include <windows.h>
#include <iostream>
using namespace std;
main(){
cout << "Running Borderless Window..." << endl;
system("BorderlessWindowed.exe");
cout << "Running Diablo II MultiRes..." << endl;
system("D2MultiResGame.exe.lnk");
}
It's just a small program to run Diablo II + a BorderlessWindow program.
this will do the task
#include <windows.h>
#include <iostream>
using namespace std;
main(){
cout << "Running Borderless Window... and Diablo II MultiRes" << endl;
system("cmd /c start BorderlessWindowed.exe&&D2MultiResGame.exe.lnk");
// this is what i have tried
// system("cmd /c start notepad.exe&&mspaint.exe");
// which starts notepad and mspaint one after another
}
Alright since system() requires that the first process be done with before it launched the second I just created a batch file that starts both, and had the .exe launch the batch file.