2017-02-07 28 views
0

我想創建一個有組織的看圖,每個食物在每個月下的金額。我能排隊第一排,但在此之後,一些事情開始了。這是我結束了:Current Output使用setw()的格式化()C++

這是我試着用setw()玩的代碼,但我似乎無法改變它的數字後,它放出第一個數組元素。

void sales_report() 
{ 
for (int i = 0; i < 7; i++) 
{ 
    cout << setw(17) << months[i] << " "; 
} 

cout << endl << foods[0] << " "; 
for (int i = 0; i < 6; i++) 
{ 
    sumapple += apples[i]; 
    cout << setprecision(2) << fixed << setw(8) << money << apples[i] << " "; 
} 
cout << setprecision(2) << fixed << setw(8) << money << sumapple << endl; 
cout << foods[1] << " "; 

for (int i = 0; i < 6; i++) 
{ 
    sumoranges += oranges[i]; 
    cout << setprecision(2) << fixed << setw(7) << money << oranges[i] << " "; 
} 
cout << setprecision(2) << fixed << setw(7) << money << sumoranges << endl; 
cout << foods[2] << " "; 

for (int i = 0; i < 6; i++) 
{ 
    sumpears += pears[i]; 
    cout << setprecision(2) << fixed << setw(10) << money << pears[i] << " "; 
} 
cout << setprecision(2) << fixed << setw(10) << money << sumpears << endl; 
cout << foods[3] << " "; 

for (int i = 0; i < 6; i++) 
{ 
    sumtomatoes += tomatoes[i]; 
    cout << setprecision(2) << fixed << setw(7) << money << tomatoes[i] << " "; 
} 
cout << setprecision(2) << fixed << setw(7) << money << sumtomatoes << endl; 
cout << foods[4] << " "; 

for (int i = 0; i < 6; i++) 
{ 
    sumcherries += cherries[i]; 
    cout << setprecision(2) << fixed << setw(6) << money << cherries[i] << " "; 
} 
cout << setprecision(2) << fixed << setw(6) << money << sumcherries << endl << totalmonth; 
for (int i = 0; i < 6; i++) 
{ 
    total = apples[i] + oranges[i] + pears[i] + tomatoes[i] + cherries[i]; 
    cout << setprecision(2) << fixed << setw(9) << money << total << " "; 
} 

}

回答

1

您在每個for循環使用不同的std::setw()值。因此錯位。使用相同的值。

+0

這適用於間距問題,但如果我保持它們都一樣我如何化妝的事實,食品名稱是不同的長度 –

+0

例如使用'std :: setw(15)'時打印出那些太。您目前不設置寬度。 –

+0

我的老師不會允許。他說我們只允許對齊,如果我對這些應用setw,那麼它會將該詞向右移動。我知道使用左側會很容易,而且在輸出中看起來很好,但不允許這樣做。 –