溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ostream類的3個輸出流對象cout,cerr,clog區(qū)別和使用

發(fā)布時間:2020-04-04 02:48:01 來源:網(wǎng)絡(luò) 閱讀:1140 作者:巖梟 欄目:編程語言

ostream類定義了3個輸出流對象:cout,cerr,clog。

cerrclog都是標(biāo)準(zhǔn)錯誤流,其區(qū)別是:cerr不經(jīng)過緩沖區(qū)直接向顯示器輸出信息;clog中的信息存放在緩沖區(qū),緩沖區(qū)滿后或遇endl向顯示器輸出。

例:求解一元二次方程,若公式出錯,用cerr流輸出有關(guān)信息。

:程序:

#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;

}

運(yùn)行結(jié)果1

please input a,b,c:0 2 3

a is equal to zero,error!

請按任意鍵繼續(xù). . .

運(yùn)行結(jié)果2

please input a,b,c:5 2 3

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

請按任意鍵繼續(xù). . .

運(yùn)行結(jié)果3

please input a,b,c:1 2.5 1.5

x1=-1

x2=-1.5

請按任意鍵繼續(xù). . .


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI