Ostream class of 3 output stream objects Cout,cerr,clog differences and usage

Source: Internet
Author: User

the Ostream class defines 3 output Stream objects:cout,cerr,clog.

Cerr and clog are standard error streams, the difference is:cerr not through the buffer directly to the display output information;Clog In the buffer, when the buffer is full or Endl output to the monitor.

Example: Solve a two-time equation, if the formula error, with cerr flow output information.

Solution : Program:

#include <iostream>

#include <cmath>

using namespace Std;


int main ()

{

Float A, B, C, disc;

cout << "Please input a,b,c:";

Cin >> a >> b >> c;

if (a = = 0)

{

Cerr << "A is equal to zero,error!" << Endl;

}

else if ((disc = B*b-4*a*c) < 0)

{

Cerr << "disc = b*b-4*a*c< 0,error!" << Endl;

}

Else

{

cout << "x1=" << (-B + sqrt (disc))/(2 * a) << Endl;

cout << "x2=" << (-b-sqrt (disc))/(2 * a) << Endl;

}

System ("pause");

return 0;

}

Run result 1:

Please input a,b,c:0 2 3

A is equal to zero,error!

Please press any key to continue ...

Run result 2:

Please input A,b,c:5 2 3

Disc = b*b-4*a*c< 0,error!

Please press any key to continue ...

Run result 3:

Please input A,b,c:1 2.5 1.5

X1=-1

x2=-1.5

Please press any key to continue ...


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1763583

Ostream class of 3 output stream objects Cout,cerr,clog differences and usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.