[英]C++ How to cin and cout in same line?
So I'm trying to code a program for my project and I'm pretty much done.所以我正在尝试为我的项目编写一个程序,我已经完成了。 Now all I want to do is make the console look a little more designed for example:现在我想做的就是让控制台看起来更有设计感,例如:
cout<<"\t\t\t\t\t\t Please Login With Admin Credentials!"<<endl;
cout<<"\t\t\t\t\t\t ==================================="<<endl;
cout<<"\t\t\t\t\t\t + [=] Enter Username: +"<<endl;
cout<<"\t\t\t\t\t\t + [=] Enter Password: +"<<endl;
cout<<"\t\t\t\t\t\t ==================================="<<endl;
Now I want the user to input username and password in this specific piece of somewhat of a design.现在我希望用户在这个特定的设计中输入用户名和密码。 As you can see I want the username and password to be input before the +.如您所见,我希望在 + 之前输入用户名和密码。 Is it possible?可能吗?
You can't use backspace to get back to previous lines, but you can use cin
during execution.您不能使用退格键返回前一行,但可以在执行期间使用cin
。
If you consider the following:如果您考虑以下情况:
std::string something, something2;
cout<<" Please Login With Admin Credentials!"<<endl;
cout<<" ==================================="<<endl;
cout<<" + [=] Enter Username: +";
for (int i = 1; i <= 12; i++) {
std::cout << '\b'; // ---------- backspace 12 times
}
std::cin >> something;
cout<<" + [=] Enter Password: +";
for (int i = 1; i <= 12; i++) {
std::cout << '\b'; // ---------- backspace 12 times
}
std::cin >> something2;
cout<<" ==================================="<<endl;
Will output something like:将 output 类似于:
Please Login With Admin Credentials!
===================================
+ [=] Enter Username:hello + // takes input 1
+ [=] Enter Password:howareyou + // takes input 2
===================================
From my aspect, this is the easiest way to achieve your requirement without any code complexity.从我的角度来看,这是在没有任何代码复杂性的情况下实现您的要求的最简单方法。
I followed Alex's advice of using HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hStdout, { 1, 1 });
我按照 Alex 的建议使用HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hStdout, { 1, 1 });
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hStdout, { 1, 1 });
under Windows.h library & it works just fine.在 Windows.h 库下,它工作得很好。 Just had to figure out the coordinates for my cursor.只需要弄清楚我的光标的坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.