我有这段代码可以与 ncurses 一起使用:
#include <iostream>
#include <string>
#include <fstream>
#include <ncurses.h>
#include <cstdlib>
//char a='a';
int c=0;
bool ex = false;
void mva()
{
std::cout << "Nothing here yet, move along." << std::endl;
}
void cho()
{
std::cout << "Choose a valid option noob!" << std::endl;
}
void menu()
{
std::cout << "Welcome." << std::endl;
std::cout << "Choose an option" << std::endl;
std::cout << "1." << std::endl;
std::cout << "2." << std::endl;
std::cout << "3." << std::endl;
std::cout << "4. About" << std::endl;
std::cout << "5. exit" << std::endl;
}
void pause()
{
std::cin.get();
std::cin.ignore();
}
int main()
{
initscr();
//clear();
refresh();
//system("clear");
while (ex != true)
{
menu();
std::cin >> c;
switch (c)
{
case 1:
mva();
pause();
system("clear"); //unfortunately, there is no clean variant to this :(
break;
case 2:
mva();
pause();
break;
case 3:
mva();
pause();
break;
case 4:
std::cout << "About" << std::endl;
std::cout << "Programmed by nnmjywg." << std::endl;
pause();
break;
case 5:
std::cout << "Press enter to exit" << std::endl;
pause();
ex = true;
break;
default:
cho();
pause();
break;
}
}
endwin();
return 0;
}
它应该可以正常工作,但是当我看到这种奇怪的行格式时,我感到非常困惑。除此之外,我也看不到我输入的内容(使用 std::cin
)
最佳答案
当您的程序调用 initscr
时,ncurses(实际上是 curses 的任何实现)初始化终端模式以允许打印回车和换行(“换行”)通过他们自己。打印换行不会产生回车。
但 iostream 的 endl
只是一个换行符。当您以这种方式打印到 cout
时,您会看到楼梯。
当使用 curses 时,cout
和 cin
并不是很有用。使用 getch
(或 wgetch
)读取输入。此外,使用 curses echo
(或 noecho
)来控制它们是否回应您提供给 getch
的输入. curses echo
对 cin
没有影响(终端同样已设置为不回显输入,并且 curses 知道何时回显)。
进一步阅读:
关于c++ - ncurses:奇怪的行格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44750687/