hihocoder 练习 A+B

题目就是输入两个数,求和,看起来挺简单的一个题,我尝试了cin.get()和cin >> ,

1,

#include<stdio.h>

#include<iostream>
using namespace std;
int main()
{
int a, b;
a = cin.get();
b = cin.get();
cout << a + b << endl;
return 0;

}

结果出错;

2,

#include<stdio.h>

#include<iostream>
using namespace std;
int main()
{
int a, b;
cin >> a;
cin >> b;
cout << a + b << endl;
return 0;

}

正确;

原因:

1,">>"输入时,空白字符会被跳过,但是cin.get不会跳过,,所以第一种方法我测试了1 2(1、空格、2),所以cin.get()的分别是1和空格,;

2,查了下变量a和b发现,保存的分别是49(数字1的ASCII),32(空格的ASCII),这是因为cin.get()用于字符输入;

3,如果将a = cin.get()改写成cin.get(a),则编译就会报错,“无法将int 转化成char”或者“没有与参数列表匹配的重载函数”,原因都是因为cin.get()用于字符输入


猜你喜欢

转载自blog.csdn.net/whatwho_518/article/details/51160048