Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print "cout" before cin?

I've got a small problem and I'm not sure if there's a possible way around this with iostream code, but I thought it's worth asking anyway.

#include <iostream>
#include <string>

using namespace std;

string cmd;

int main() {

cout << "-------------" << endl;
cout << "command: ";
cin >> cmd;
cout << "-------------" << endl;
system("pause");
}

Is it anyway possible to print the 3rd cout line before the user has typed into the cmd variable? That way, the text field will be wrapped with the "-" above and below, looking like this:

-------------
command: <user would type here>
-------------

If this is not possible, could you please point me into the direction of some possible libraries that I could use to achieve this?

like image 681
open surce Avatar asked Jan 27 '26 01:01

open surce


1 Answers

There is no standard way of achieving this in C++.

Different terminals have their own capabilities and different systems have their own API's for interacting with the terminal.

The output of the bottom line must certainly be performed first before waiting for the input, but it may be possible to move the output "cursor" to some other location than the bottom of the screen. You may find details in the documentation of the system that you intend to target.

like image 81
eerorika Avatar answered Jan 29 '26 15:01

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!