C++学习笔记(一)——浅析cin、cin.get()、cin.getline.

1.使cin输入

只能读取到第一个空白符结束,Jhon Black只会读到Jhon,另外还会造成回车符号留在输入队列情况,如果cin和cin.getline()混合使用,会导致cin.getline()无法读入【cin留下的回车会被cin.getline()认为输入结束】;

#include<iostream>
using namespace std;
int main()
{
    const int size = 25;
    char name[size];
    char address[size];
    cout<<"Enter your name:";
    cin>>name;//如果输入的内容是本身包含空白符(如:“John Black”),cin会截断;
    cout<<"Enter your address:"<<endl;
    cin>>address;  //被第一个cin截断的未被输入的部分会留在输入队列,影响下一次读入
    cout<<"Your name is "<<name<<endl;
    cout<<"Your address is "<<address<<endl;
}

/*******运行结果***********

Enter your name:John Black
Enter your address:
Your name is John
Your address is Black

***************************/

2.使用cin.get()读入

读入到换行结束读入,但是会将换行\n保留在输入队列并不消耗掉,影响下一次读入;

①未消除换行影响:

#include<iostream>
using namespace std;
int main()
{
    const int size = 25;
    char name[size];
    char address[size];
    cout<<"Enter your name:\n";
    cin.get(name,size);//.get();可以完整读入姓名John Black但是会导致地址无法读入
    cout<<"Enter your address:"<<endl;
    cin.get(address,size);
    cout<<"Your name is "<<name<<endl;
    cout<<"Your address is "<<address<<endl; 
}
/*******运行结果**********

Enter your name:
John Black
Enter your address:
Your name is John Black
Your address is

*************************/

②可以用cin.get()读入一个换行,来消除对下一次输入的影响:

#include<iostream>
using namespace std;
int main()
{
    const int size = 25;
    char name[size];
    char address[size];
    cout<<"Enter your name:\n";
    cin.get(name,size).get();//其中的cin.get可以读入换行符,不会导致下一次输入失效
    //也可以分两行写
    //cin.get(name,size);
    //cin.get();
    cout<<"Enter your address:"<<endl;
    cin.get(address,size);
    cout<<"Your name is "<<name<<endl;
    cout<<"Your address is "<<address<<endl; 
}

/********运行结果*********

Enter your name:
Jhon Black
Enter your address:
China
Your name is Jhon Black
Your address is China

*************************/

3.使用cin.getline()输入

也是读入一整行,但是会读入换行符,不会留在输入队列对下一次输入造成影响;

#include<iostream>
using namespace std;
int main()
{
    const int size = 25;
    char name[size];
    char address[size];
    cout<<"Enter your name:\n";
    cin.getline(name,size);//不用类似cin.get()的结构来消耗换行符,cin.getline()会自动读入                
                           //换行并且丢弃
    cout<<"Enter your address:"<<endl;
    cin.getline(address,size);
    cout<<"Your name is "<<name<<endl;
    cout<<"Your address is "<<address<<endl; 
}

/**********运行结果***********

Enter your name:
Jhon Black
Enter your address:
China
Your name is Jhon Black
Your address is China

*****************************/

不建议使用cin.getline()兼容性不如cin.get(),且不如cin.get()容易检查错误;

4.备注:关于cin.get()和cin.getline()的第三个参数

cin.get()和cin.getline()理论上都需要传入三个参数,但一般第三个参数可以省略且默认为'\0';

但是可以设置为你想要的,如以#键结束;

#include<iostream>

using namespace std;
int main()
{
    const int size = 25;
    char name[size];
    cout<<"Please enter your name:\n";
    cin.get(name,size,'#');//读到#停止读入,且不包括#
    cout<<"Your name is ";
    cout<<name;
}

/***********运行结果***********

Please enter your name:
John#
Your name is John

******************************/

参考文献:《C++Primer Plus》

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值