我想编写一个C++程序,当用户从键盘输入任何字符时,它应该移动到下一行代码。
这是我的代码:
char c;
cin>>c;
cout<<"Something"<<endl;
但是这并不起作用,因为它只有在我输入一些字符然后按下 ENTER 键时才会移到下一行。
或者
如果我使用这个:
cin.get() or cin.get(c)
当我按下 Enter 键时,它会移动到下一条指令。
但是我想让它在键盘上按任意键时都移到下一行,应该如何实现?
我想编写一个C++程序,当用户从键盘输入任何字符时,它应该移动到下一行代码。
这是我的代码:
char c;
cin>>c;
cout<<"Something"<<endl;
但是这并不起作用,因为它只有在我输入一些字符然后按下 ENTER 键时才会移到下一行。
或者
如果我使用这个:
cin.get() or cin.get(c)
当我按下 Enter 键时,它会移动到下一条指令。
但是我想让它在键盘上按任意键时都移到下一行,应该如何实现?
在Windows上:
system("pause");
并且在 Mac 和 Linux 上:
system("read");
将输出“按任意键继续......”,并等待按下任意键。 我希望这是您所想要的。
如果你使用的是Windows系统,可以使用kbhit()
函数, 这个函数是Microsoft运行时库的一部分。如果你使用的是Linux系统,可以按照以下方式实现kbhit
函数(来源):
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
更新: 上面的函数在OS X上正常工作(至少在OS X 10.5.8-Leopard上,所以我希望它也能在更高版本的OS X上工作)。这个代码片段可以保存为kbhit.c
并在Linux和OS X上编译。
gcc -o kbhit kbhit.c
当使用以下方式运行时
./kbhit
这会提示您按下一个键,并在按下键后退出(不仅限于Enter或可打印的键)。
@Johnsyweb - 请详细说明您所说的“详细规范答案”和“所有问题”。此外,“跨平台”:使用此 kbhit()
实现,您可以在Linux / Unix / OS X / Windows上的C ++程序中拥有相同的功能-您可能指的是哪些其他平台?
@Johnsyweb的进一步更新: C ++应用程序并未在封闭的C ++环境中运行。 C ++之所以成功的一个重要原因就是与C的互操作性。所有主流平台都是使用C接口实现的(即使内部实现是使用C ++),因此您谈到的“遗留”似乎不合适。另外,由于我们正在讨论一个单一的函数,您为什么需要C ++(“带类的C”)?正如我指出的那样,您可以在C ++中编写并轻松访问此功能,而您应用程序的用户不太可能关心您是如何实现它的。
kbhit()
函数在一行中声明了一个terminal_settings
变量,这似乎什么也没做,但它却做了一切。有些人可能认为这很好,但我觉得它太晦涩难懂了。 - Vinay Sajip没有完全可移植的解决方案。
问题19.1在comp.lang.c FAQ中对此进行了详细说明,并提供了Windows、类Unix系统、甚至MS-DOS和VMS的解决方案。
以下是快速但不完整的摘要:
cbreak()
,然后调用getch()
(不要与Windows特定的getch()
函数混淆)。请注意,通常会控制终端,因此这可能过于复杂。ioctl()
来操作终端设置。tcgetattr()
和tcsetattr()
可能是更好的解决方案。system()
调用stty
命令。getch()
或getche()
。SMG$
)例程可能起作用。所有这些C语言解决方案同样适用于C++;我不知道任何专门针对C++的解决方案。
#include
之外没有任何预处理器。然后,在构建时只需选择正确的实现文件,就完成了。 - BitTicklergetch
暂停控制台直到按下一个键(https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/)。#include<iostream.h>
#include<conio.h>
using namespace std;
void check()
{
char chk; int j;
cout<<"\n\nPress any key to continue...";
chk=getch();
j=chk;
for(int i=1;i<=256;i++)
if(i==j) break;
clrscr();
}
void main()
{
clrscr();
check();
cout<<"\n\nIt works!";
getch();
}
getch()
与cin.get
或cin>>
有何不同,可以附上相关文档链接。 - user2622016#include <iostream>
#include <conio.h>
// ...
// ...
// ...
cout << "Press any key to continue..." << endl;
_getch();
cout << "Something" << endl;
我了解你想要实现的内容,因为我记得我也曾经想过做同样的事情。受到Vinay的启发,我写了一些对我有效并且我有点理解的东西。但我不是专家,请小心使用。
我不知道Vinay怎么知道你在使用Mac OS X。但是大多数类Unix操作系统应该可以像这样工作。真正有用的资源是opengroup.org
在使用函数之前,请确保刷新缓冲区。
#include <stdio.h>
#include <termios.h> //termios, TCSANOW, ECHO, ICANON
#include <unistd.h> //STDIN_FILENO
void pressKey()
{
//the struct termios stores all kinds of flags which can manipulate the I/O Interface
//I have an old one to save the old settings and a new
static struct termios oldt, newt;
printf("Press key to continue....\n");
//tcgetattr gets the parameters of the current terminal
//STDIN_FILENO will tell tcgetattr that it should write the settings
// of stdin to oldt
tcgetattr( STDIN_FILENO, &oldt);
//now the settings will be copied
newt = oldt;
//two of the c_lflag will be turned off
//ECHO which is responsible for displaying the input of the user in the terminal
//ICANON is the essential one! Normally this takes care that one line at a time will be processed
//that means it will return if it sees a "\n" or an EOF or an EOL
newt.c_lflag &= ~(ICANON | ECHO );
//Those new settings will be set to STDIN
//TCSANOW tells tcsetattr to change attributes immediately.
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
//now the char wil be requested
getchar();
//the old settings will be written back to STDIN
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}
int main(void)
{
pressKey();
printf("END\n");
return 0;
}
O_NONBLOCK似乎也是一个重要的标志,但对我来说它并没有改变什么。
如果有一些更深入的知识的人能够评论并给出一些建议,我将不胜感激。
如果你使用的是Visual Studio 2012或更早版本,请使用getch()
函数,如果你使用的是Visual Studio 2013或更高版本,请使用_getch()
函数。你需要使用#include <conio.h>
。示例:
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Press any key to continue. . .\n";
_getch(); //Or getch()
}
这适用于Windows平台: 它直接使用微处理器寄存器,可用于检查按键或鼠标按钮。
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
clrscr();
union REGS in,out;
in.h.ah=0x00;
printf("Press any key : ");
int86(0x16,&in,&out);
printf("Ascii : %d\n",out.h.al);
char ch = out.h.al;
printf("Charcter Pressed : %c",&ch);
printf("Scan code : %d",out.h.ah);
getch();
}
/* getchar example : typewriter */
#include <stdio.h>
int main ()
{
char c;
puts ("Enter text. Include a dot ('.') in a sentence to exit:");
do {
c=getchar();
putchar (c);
} while (c != '.');
return 0;
}