应对c++不让用gets()函数

最近,c++正规考试好像不让用gets()函数了,可能大家就要慌了:众所周知gets()函数是一种很快的输入方式,我们不用可能会超时,来,我告诉你

#include<iostream>
#include<conio.h>
//注意:这里必须得用conio.h头文件,不然getch()函数就用不了
using namespace std;
int main(){
    char a;
    int an;
    while(true){
        if(_kbhit()){
            a=_getch();
            an=a;
            //以下为对字符串进行处理、存储
            //到这
            if(an==13){
                break;
            }
        }
    }
    //然后干什么事自己定
    return 0; 
}

看起来非常简单:

以下为不加注释的源代码:

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
    char a;
    int an;
    while(true){
        if(_kbhit()){
            a=_getch();
            an=a;
            if(an==13){
                break;
            }
        }
    }
    return 0; 
}

考虑到有的人没登录,以下为源代码:

#include<iostream>

#include<conio.h>

//注意:这里必须得用conio.h头文件,不然getch()函数就用不了

using namespace std;

int main(){

char a;

int an;

while(true){

if(_kbhit()){

a=_getch();

an=a;

//以下为对字符串进行处理、存储

//到这

if(an==13){

break;

}

}

}

//然后干什么事自己定

return 0;

}

不加注释的:

#include<iostream>

#include<conio.h>

using namespace std;

int main(){

char a;

int an;

while(true){

if(_kbhit()){

a=_getch();

an=a;

if(an==13){

break;

}

}

}

return 0;

}

怎么样?够义气吧?给个赞吧

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值