Ive been writing the folowing code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
cout << "The script will be executed";
system("./simple.sh");
}
But when I run it the shell script is executed first.
What can I do to execute "cout << "The script will be executed"" first?
Flushing the output stream buffer should be enough. You can do this with
cout << "The script will be executed";
cout.flush();
Alternatively, if you intended to also print a newline character then you can use std::endl
which implicitly flushes the buffer:
cout << "The script will be executed" << endl;
You're not flushing the output stream.
Try:
cout << "The script will be executed" << endl; // or cout.flush()
system("./simple.sh");
The script is executed second, the delay between the call to cout
and printing to the console is probably throwing you off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With