c语言中的变量默认值,使用cin >>的C中变量的默认值

自C11标准起,std::basic_istream::operator>>在提取失败或数值溢出时的行为有所改变。当输入无效或数值过大/小无法存储时,会设置错误标志并可能修改变量值。例如,读取姓名和年龄时,如果输入无效,应通过clear()函数重置流状态。
摘要由CSDN通过智能技术生成

std::basic_istream::operator>>的行为已从C 11改变.自C 11以来,

If extraction fails, zero is written to value and failbit is set. If

extraction results in the value too large or too small to fit in

value, std::numeric_limits::max() or std::numeric_limits::min()

is written and failbit flag is set.

请注意,直到C 11,

If extraction fails (e.g. if a letter was entered where a digit is

expected), value is left unmodified and failbit is set.

string first_name;

if (!(cin>>first_name)) {

first_name = "???";

cin.clear(); //Reset stream state after failure

}

int age;

if (!(cin>>age)) {

age = -1;

cin.clear(); //Reset stream state after failure

}

cout<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值