C++ commands executing out of order

You should try "flushing" the output buffer to make sure it prints in order. Try:

cout << "SweetShell-> " << std::flush;
int test = read(0,buf,MAX_ARGS);
//temp is a string that is set to the input
cout << temp << "    " << test << std::flush;

Because the output is buffered, you need to flush the output before trying to read() your input.

Incidentally, be careful when combining raw OS-level read(2) and write(2) operations with buffered IO operations; while you can certainly use them both in the same program, using them both on the same file or socket is going to create trouble; so sticking with one form or the other will reduce the likelihood of introducing flaws in the future.


The crucial thing is that std::cout and std::cin are tied (see http://www.cplusplus.com/reference/iostream/ios/tie/) - this means that streaming operations on std::cin will first trigger a flush on std::cout. But, you're using the libC read(...) function which bypasses the C++ streams library altogether, therefore there's no chance for the flush to be invoked. You could use std::cin.read() instead.