2017-02-07 3 views
0

私は、毎月の各食品の金額を体系的に見たグラフを作成しようとしています。私は最初の行を並べることができましたが、その後何かが始まりました。これは私が終わったものです:Current Outputsetw()で書式設定する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

私の先生は許可しません。彼は私たちが右に整列することが許されていると言いました、そして、私がそれらに人に住むことを適用すれば、それは単語を右に動かすでしょう。私は左を使うのは簡単だろうと知っています。出力にはうまく見えますが、これは許されません。 –