gpt4 book ai didi

c++ - 忽略 "cin"并使用 "kbhit"转到另一个函数

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

我需要更多使用 C++ 的帮助,假设我不知道自己多大了,我想按 ESC 键返回到“function2”。我想要一些东西,当我按 ESC 时(什么时候无关紧要)它会忽略“cin”并转到“function2”。(我知道我不需要所有的库)

#include <iostream>
#include <math.h>
#include <windows.h>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
# include <conio.h>
using namespace std;


int function2();
float a, c;

int main(){

do {
while (kbhit())
{c = getch();}

if (c==27)
{function2();}

cout << "How old are you?\t" << c << endl;
cin>>a;


} while(c != 27);}


int function2(){
cout<< "!!!END!!!\n";
return 0;
}

最佳答案

conio.h 是一个已弃用的非标准 C 库。为了从输入中获取字符,您必须通过 cin(例如 cin.get()),或者使用系统相关的功能,在这种情况下您需要查看编译器为您的平台提供的库。如果可用,请尝试 getch()(另一个不可移植的函数)。

At this site您可以找到几个关于如何实现所需内容的示例。

关于c++ - 忽略 "cin"并使用 "kbhit"转到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5602706/

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