谈C++求a+b(大神勿喷)

刚学C++,发现C++的输入输出不再是printf与scanf,而是cout和cin,下面我由一道题来详细说明一下!

参照hduoj第1089题:求a+b的和。

#include <iostream>

using namespace std;//C++头文件

{

int a,b;

while(cin>>a>>b)//相当于C语言中的while(scanf("%d %d",&a,&b)!=EOF)

{

cout<<a+b<<endl;//输出a+b的和,endl相当于C语言中的

}

return 0;

}

其中输出也可以这样写:cout<<"sum="<<a+b<<endl;(需要提前定义sum)。

此外还要注意cin后跟“>>",cout后跟"<<"。

原文地址:https://www.cnblogs.com/cnlik/p/11851930.html