0

我下面的代码在启动时什么也不输出。当我输入 1,2,3 时,它会打印以下内容:

1Enter the value of argv[1]:49
2Enter the value of argv[2]:50
3Enter the value of argv[3]:51

我对使用循环时应该将 refresh() 放在哪里感到很困惑。我正在尝试实现类似于 for 循环内的注释。

int main()
{
    initscr();
    int argv[3];
    int argvLen = sizeof(argv)/sizeof(*argv);

    for (int i=0; i<argvLen; i++)
    {
        int n = getch();
        printw("Enter value of argv[%d]: %d \n", i+1, n);
        argv[i] = n;
        refresh();

        //cout << "Enter value of argv[" << i+1 << "]:" << endl;
        //cin >> argv[i];
    }

    endwin();
    return 0;
}
4

4 回答 4

0

getch()返回一个字符,例如'1','2''3'. 它们的整数值为 49、50、51。如果需要整数值,则应减去'0'

int n = getch() - '0';

请注意,这只适用于数字(0 到 9)。如果您输入任何其他内容,它不会给您预期的答案,因此您可能希望在此处添加额外的检查。

于 2013-09-04T08:44:59.423 回答
0

假设您处于延迟模式,getch()将阻止等待用户输入。

首先做你printw()在屏幕缓冲区中放一些东西,然后做你refresh()使修改后的屏幕缓冲区可见,然后做getch().

于 2013-09-04T08:45:05.423 回答
0

您可以使用 scanf 和相关函数来读取整数值。

int main()
{
    int i;
    int values[3];
    int valuesLen = sizeof(values)/sizeof(*values);

    for (i=0; i<valuesLen; i++)
    {
        printf("Enter value of values[%d]: ", i+1);
        scanf("%d", &values[i]);
        printf("Value of values[%d]: %d \n", i+1, values[i]);                
    }

    return 0;
}
于 2013-09-04T10:07:57.333 回答
0

No explicit call to refresh is needed, because getch does a refresh, as noted in the manual page:

If the window is not a pad, and it has been moved or modified since the last call to wrefresh, wrefresh will be called before another character is read.

As written, the program does not appear to display anything because initially curses is in cooked mode. Adding calls to cbreak and noecho after the call to initscr would let individual characters be entered (again, see manual page for ncurses).

With those changes, there is an additional problem: the last printw will not be displayed. Putting a getch() just before the endwin() fixes that.

于 2015-04-19T12:59:17.833 回答