从标准输入读取字符,而无需等待C ++中的换行符
尚不存在可移植的解决方案。在Windows上,可以使用getch()conio(ConsoleI/O)库中的函数来按下字符。
示例
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
char c;
while(1){ // infinite loop
c = getch();
cout << c;
}
}这将输出您输入到终端的任何字符。请注意,这仅在Windows上有效,因为conio库仅在Windows上存在。在UNIX上,您可以通过以系统原始模式输入来实现。
示例
#include<iostream>
#include<stdio.h>
int main() {
char c;
//将终端设置为原始模式
system("stty raw");
while(1) {
c = getchar();
// terminate when "." is pressed
if(c == '.') {
system("stty cooked");
exit(0);
}
std::cout << c << " 被按下。"<< std::endl;
}
}热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短