C + + first glimpse of IO

(1) Do you really understand the "cout <<endl;" significance?

Many people know that endl is a special value, called a manipulator (manipulator). Its role is to wrap the output results. But just more than that, it should refresh the buffer associated with the device (buffer). When we cout <<"a string show" when the string is written to the output device of the buffer, the user can not see the screen in time string. But to wait until the data buffer is flushed before they are reflected to the external device. The output of endl will flush the buffer in time.

This is a very important thing. We programmers often insert the output statements in the code for debugging. Since this time will be great use of endl. Consider the following code:

#include<iostream.h>

int main(){
    int p[100];
    cout<<"there is error below!";  // Standard output
    p[101]=1;  // Error location, the array is out of range
    return 0;
}

Obviously, the run to the line 6 has a runtime error (array bounds checking by the compiler will not come). But the supposedly should print out "there is error below" error after the. So that we can quickly navigate to the following error appears in the standard output. However, the results of running nothing but the output? We changed a bit code to the output line 5 changed: cout <<"there is error below!" <<Endl; can see the strings on the screen, and it will be too small, it appears the system run error.

Sum up: we insert the output of debugging statements, should be promptly flushed the output stream. Forgotten endl may result in the output string to stay in the buffer, if the program crashes, it will lead to erroneous inference ending crash.

(2) the characteristics of standard input?

#include<iostream.h>

int main(){
     int v1,v2;
     cin>>v1>>v2;  // Enter two integer
     cout<<v1+v2<<endl;
     return 0;
}

We can screen through this in several ways to enter two integer variables v1 and v2:

分类:CPP 时间:2010-04-27 人气:222
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.609 (s). 14 q(s)