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