【发布时间】:2021-03-31 11:09:09
【问题描述】:
代码:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
cout << "Enter a string : " << endl;
cin >> s;
cout << "The Entered String is : " << s << endl;
cout << "The Length of Entered String is : " << s.length() << endl;
return 0;
}
输出:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $g++ -o trycpp -Os try.cpp
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
如果你想要 C 中的代码,C 也会发生同样的事情,请索取!
当我按箭头键时 ^[[C 出现而不是光标移动(其他箭头键、转义键、home、end 发生类似的事情)!
这里发生的事情是字符串第二次包含字符:
['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']
所以,'\x1b', '[', 'C' 是从键盘发送到外壳的字符序列,用于表示右箭头键(向前光标)。
我想要的是这些字符不会显示在外壳中,但光标会移动(根据按下的键向前、向后、到家、结束等)。
输入后处理意义不大,主要目的是让光标移动!
如何在 C 或 C++ 中实现这一点?
[编辑]
唯一的目标是在使用程序时为用户提供与终端一样的体验。
【问题讨论】:
-
您需要控制终端,例如使用ncurses 或类似的库。使用 C++ 标准流是不可能的。
-
您需要使用合适的第三方库,例如
ncurses。 -
@Someprogrammerdude 我正在学习 C++ 你能解释一下如何使用它吗