相关疑难解决方法(0)

为什么getchar()不等我在scanf()后按Enter键?

我正在学习C,我正在使用"getchar()"来停止命令窗口,所以我可以看到练习正在进行,但它只是不起作用.下面是一个样本:

#include <stdio.h>

int main()
{
    int value;
    printf("1. option 1.\n2. option 2.\n3. option 3.\n4. Exit\n\nMake an option: ");
    scanf("%d", &value);
    switch (value)
    {
        case 1:
            printf("you selected the option 1.");
            break;
        case 2:
            printf("you selected the option 2.");
            break;
        case 3:
            printf("you selected the option 3.");
            break;
        case 4:
            printf("goodbye");
            break;
        default:
            printf("thats not an option");
            break;
    }
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

  1. 选项1.
  2. 选项2.
  3. 选项3.
  4. 出口.

做出选择:1

您选择了选项1.

进程返回0(0x0)执行时间:3.453秒

按任意键继续.

为什么不等待"getchar()"的输入?

c getchar

16
推荐指数
3
解决办法
4万
查看次数

等待C中的用户输入?

我正在尝试创建一个暂停用户输入的简单命令.我认为它在Bash脚本中很有用.

这是我的代码:

#include <stdio.h>
int main() {
  char key[1];
  puts("Press any key to continue...");
  fgets(key,1,stdin);
}
Run Code Online (Sandbox Code Playgroud)

它甚至不会暂停用户输入.

我之前尝试过使用getch()(ncurses).发生的事情是,屏幕变成空白,当我按下一个键时,它又回到原来在屏幕上的内容,我看到:

$ ./pause
Press any key to continue...
$
Run Code Online (Sandbox Code Playgroud)

这有点像我想要的.但我想要的只是pauseDOS/Windows 中的命令(我使用的是Linux).

c linux

3
推荐指数
1
解决办法
3万
查看次数

用"fprintf"打印错误后调用"getchar()"有什么意义?

我目前正在学习使用OpenGL,并开始关注http://www.opengl-tutorial.org/上的一系列教程.

在第一个教程中,他们使用这个if语句开始main():

// Initialize GLFW
if (!glfwInit() ) 
{
  fprintf( stderr, "failed to initialize GLFW\n" );
  getchar();
  return -1;
}
Run Code Online (Sandbox Code Playgroud)

据我所知,fprintf是一种无缓冲的打印,是指示错误的标准.返回-1也有意义.但是在指出错误后调用getchar()有什么意义呢?

谢谢.

c++

1
推荐指数
1
解决办法
71
查看次数

标签 统计

c ×2

c++ ×1

getchar ×1

linux ×1