I have a small problem with the clear command in C++. The Clear function is supposed to clear everything at the end of the program but it clears too early. Does anyone know how to solve that problem and let the program clear it as the complete end? I want that at the end the console is empty.
#include <iostream>
#include <chrono>
#include <thread> // sleep_for, sleep_until
#include <ctime>
#include <thread>
using namespace std;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono;
int main() {
cout<<"\nTest message?";
sleep_for(nanoseconds(1500000000));
system("clear");
cout<<"\nThis message should be cleared";
sleep_for(nanoseconds(5000000000));
system("clear");
}
Console output:
This message should be clearedîș§
standard cout in c++ is buffered.
this means that the text is held before it is sent to the screen, this helps with performance.
this is causing the clear to be called before the buffer is sent to the screen.
to fix this you need to manually flush the buffer by either putting << std::flush or << std::endl at the end of the cout statement
Related
I am trying to show in an edit box a program execution time. I have found some examples at Stackoverflow like the code here below.
using namespace std;
using namespace date;
ostringstream out;
auto start = chrono::system_clock::now();
//some program execution
auto finish = chrono::system_clock::now();
out << finish - start;
string s = out.str();
cout << s << '\n';
I have installed the library #include <date/date.h> via vcpkg. But the problem is: as soon as I do #include <date/date.h>, and run code with Local Windows Debugger a numerous mistake is happening as indicated in the snap shot below.
I mean, simple including of <date/date.h> library leads to errors.
How can I avoid this issue?
Many thanks in advance!
UPD
#include "pch.h"
#include "framework.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <date/date.h>
void CMFCApplication2Dlg::OnBnClickedButton1()
{
//I have cleared the code inside, but errors yet appear
}
I tried using unistd.h sleep(x) on Linux and if the stream isn't explicitly flushed, it'd hang up for the defined time and then output all cout statements at once. On other hand, when using Windows.h header file on my Windows OS, it'd actually wait that amount of time and output each cout statement as if they were individually flushed.
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
cout << "Test";
Sleep(2000);
cout << "Test";
}
So, is it implemented or am I wrong about this?
I am new to trying to write anything. While I can read what's happening most of the time, I have no idea how to build a delay. In Arduino I have used delays but it doesn't seem to work the same here.
I have been searching the internet trying to find something that will work, but with no luck. I think I could make something work but I don't know how to add more '#includes' either. Currently I have-
#include <xbee_config.h>
#include <types.h>
#include <utils.h>
#include <xbee/atcmd.h>
I have the general idea of what is needed but now idea how to write it. I'm turning on an LED that I need delayed before turning off.
gpio_set(LED1, 1); //Turn on LED
**Delay here!!!!**
gpio_set(LED1, 0); //Turn off LED
My first thought is building a void_delay function that will increment a counter until x time is reached, then return to the program. I know that's not the best way as it will be keeping the program from other tasks while counting, but it should work for my purpose. The problem, I have no idea how to write that.
In c++ you can use Sleep(milliseconds) you only have to include <windows.h>.
Example:
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
cout << "Before delay" <<endl;
Sleep(5000);
cout << "After delay" <<endl;
return 0;
}
I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.
This is test example:
(1). simple program doing endless loop:
#include <iostream>
using namespace std;
int main() {
int counter = 0;
while (1) cout << ++counter << ": endless loop..." <<endl;
}
(2). another program that launches above example through system() command:
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
system("endless_loop.exe");
cout << "back to main program" << endl;
}
When doing Ctrl+Break on this program text back to main program doesn't show.
How to restrict this key combination to inside process and return execution pointer back to main app ?
Another thing is that I don't always have control over source code of inside program, so I can't change things there.
Add this::
#include <signal.h>
...
signal (SIGINT, SIG_IGN);
After the signal() call, the program ignores Ctrl-Break. On Linux, ignoring signals propagates to child processes through fork()/exec().
I would expect Windows to reset the default signal handling on exec() due to the way the O/S + runtime library work. So if you want the child to ignore Break, add the code above to it too.