2011-10-11 56 views
2

我想用setw來清理我的程序的輸出。我想要「訂購的假脫機總數」和輸出之間的空白空間。C++ setw移動整行不只是需要的部分

編輯這是我去爲:

example output

,這就是我得到 enter image description here 這裏是我到目前爲止有: 更新的代碼

/********************************************/ 
// Name: results       /
// Description: Print results    /
// Parameters: N/A       /
// Reture Value: N/A      /
/********************************************/ 
void results(int spoolnumber, int subtotalspool, float shippingcost, float totalcost) 
{ 
    cout << left << setw (45) << "Total number of spools to be ordered is: " << right << spoolnumber << endl << endl; 

    cout << left << setw (45) << "The subtotal for the spools is:" << right << "$" << subtotalspool << endl << endl; 

    cout << "The shipping cost is: $" << shippingcost << endl << endl; 

    cout << "The total cost is: $" << totalcost << endl << endl; 

    return; 
} 
+0

羅布的規則#8:當你的意思是''\ n''時不要說'endl'。參見[endl fiasco](http://stackoverflow.com/questions/5492380/what-is-the-c-iostream-endl-fiasco/5492605#5492605)。 –

+0

+1根據反饋澄清問題 –

+0

@ Steve Townsend:問題越好,答案越好。 –

回答

4

你可以也做

cout << left << setw (45) << "Total number of spools to be ordered is: " << spoolnumber << endl << endl; 

選擇填充進行的哪一側。默認值是左側。

編輯:使用字符串流

stringstream ss; 
ss << "$" << spoolnumber 

我想你可以通過添加其他運輸及工務局局長修復右端。所以:

cout << left << setw (45) << "Total number of spools to be ordered is: " << right << setw(5) << ss.str() << endl << endl; 
+0

也不工作,如果這有助於使我的問題更精確,我包含一個快速示例圖像。編輯:這是多數民衆贊成的權利,我需要使用左和不正確 –

+0

工作有點,現在零不排隊。 http://imageshack.us/photo/my-images/198/34267038.png/我甚至在文本字符串後添加了<< right <<,所以$和數字在右邊 –

+0

編輯只是把它移到了更多 –