ncurses:奇怪的行格式

Dis*_*ics 4 c++ ncurses

我有这个代码可以与 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;
}
Run Code Online (Sandbox Code Playgroud)

它应该可以正常工作,但是当我看到这种奇怪的行格式时,我绝对感到困惑。除此之外,我也看不到我输入的内容(带有std::cin奇怪的行格式

Tho*_*key 5

当您的程序调用时initscr,ncurses(实际上是curses的任何实现)会初始化终端模式以允许自行打印回车符和换行符(“换行符”)。打印换行符不会产生回车符。

但 iostreamendl只是换行。当您以这种方式打印时cout,您会看到楼梯。

当使用诅咒时,coutcin没有真正有用。使用getch(或wgetch) 读取输入。另外,使用curses echo(或noecho)来控制它们是否回显您给的输入getch。cursesecho没有影响cin(终端同样已设置为不回显输入,curses 知道何时回显)。

进一步阅读: