《C++ primer 5》 chapter 1.2

1.2 input and output

note:

C++ language does not define any statements to do IO. Instead, C++ include an extensive standard library that provides IO.

Fundamental to the iostream library are two types named istream and ostream, which represent input and output streams.

The object of type istream named cin.

The object of type ostream named cout, cerr, clog.


The << operator takes two operands : the left-hand operand must be an ostream object, the right hand operand is a value to print. The operator writes the given value on the given ostream. The result of the output operator is its left-hand operand.

std::cout<< "hello"; //string literal
std::cout<<std::endl; //manipulator

endl has the effect of ending the current line and flushing the buffer associated with that devise

std::cin>>v1>>v2;

The input operation reads two values from std::cin, storing the first in v1 and the second in v2.


Using names from standard library: the prefix std:: indicates that the names like cout is defined inside the namespace named std.

Avoid inadvertent collisions between the name we defined and the name inside a library.

All the names defined by the standard library are in the std namespace.


exercise:

exercise 1,3

<span style="font-size:14px;">#include<iostream>

int main()
{
	std::cout<<"Hello, World"<<std::endl;
	system("pause");
	return 0;
}</span>

exercise 1.4

<span style="font-size:14px;">#include<iostream>

int main()
{
	int a=0,b=0;
	std::cin>>a>>b;
	std::cout<<a*b<<std::endl;
	system("pause");
	return 0;
}</span>

exercise 1.5

<span style="font-size:14px;">std::cout<<"The sum of ";
	std::cout<<v1;
	std::cout<<" and ";
	std::cout<<v2;
	std::cout<<" is ";
	std::cout<<v1+v2;
	std::cout<<std::endl;</span>

exercise 1.6

This program is illegal because there is no ostream object in the second and the third statement.

You can add std::cout to the second and the third statement.

Or you can just remove  ";" from the first and the second line.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值