问题
I have this code to work with 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;
}
It should work normally, but I'm absolutely baffled when I see this strange line formatting. In addition to that, I also can't see what I enter (with std::cin
)
回答1:
When your program calls initscr
, ncurses (actually any implementation of curses) initializes the terminal modes to allow printing carriage-return and line-feed ("newline") by themselves. Printing a line-feed will not produce a carriage-return.
But iostream's endl
is just a line-feed. When you print to cout
in that way, you'll see staircasing.
When using curses, cout
and cin
are not really useful. Use getch
(or wgetch
) to read input. Also, use the curses echo
(or noecho
) to control whether those echo the input which you give to getch
. The curses echo
has no effect on cin
(the terminal likewise has been set to not echo input, and curses knows when to echo).
Further reading:
- Getch() incompatible with display function in linux c++
来源:https://stackoverflow.com/questions/44750687/ncurses-strange-line-formatting