gpt4 book ai didi

c++ - 哪里应该使用ncurses刷新()?

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:09 26 4
gpt4 key购买 nike

下面的代码启动时不输出任何内容。当我输入 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;
}

最佳答案

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

int n = getch() - '0';

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

关于c++ - 哪里应该使用ncurses刷新()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com