c++ - ncurses:奇怪的行格式

标签 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;
}

它应该可以正常工作,但是当我看到这种奇怪的行格式时,我感到非常困惑。除此之外,我也看不到我输入的内容(使用 std::cin) Strange line formatting

最佳答案

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

但 iostream 的 endl 只是一个换行符。当您以这种方式打印到 cout 时,您会看到楼梯。

当使用 curses 时,coutcin 并不是很有用。使用 getch(或 wgetch)读取输入。此外,使用 curses echo(或 noecho)来控制它们是否回应您提供给 getch 的输入. curses echocin 没有影响(终端同样已设置为不回显输入,并且 curses 知道何时回显)。

进一步阅读:

关于c++ - ncurses:奇怪的行格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44750687/

相关文章:

c++ - 单线程程序明显使用多核

c++ - 在 C 或 C++ 中释放内存

带有二维数组的 C++ 宏

c++ - new 的内存分配

使用 ncurses 清除屏幕上的字符

c - Ncurses,非阻塞getch错过了第一个字符

c - 如何在不清除屏幕的情况下使用 getch from curses?

python - 使用 Python curses 突出显示和选择文本

python - Python 中的 N-curses : how to catch and print non ascii character?

c++ - 在 C 的不同函数中共享打开的文本文件