2016-07-03 12 views
1
私が与えられている

と割り当ては、下記のデータのファイル読み込みする:++ Cに複数行の文字列と整数を読み込み、入力ファイルとC++で表示するにはどうすればよいですか?

Turn on the Bright Lights 
Interpol 
9.49 
House of Jealous Lovers 
The Rapture 
1.29 
Fever to Tell 
Yeah Yeah Yeahs 
6.99 
Desperate Youth, Blood Thirsty Babes 
TV on the Radio 
8.91 
The Fragile 
Nine Inch Nails 
12.49 

入力このデータを、その後、マトリックスとCDのの出力合計金額とどのように多くのCDのは、販売されたに表示されます。これまでのところ、最初の行は正しく表示されるだけで、残りの部分を表示するためには何を変更する必要があるのか​​分かりません。これまでに私が書いたものがここにあります。私は出力コードを開始していないと私はそれに問題がないように感じる。

#include<iostream> 
#include<fstream> 
#include<iomanip> 
#include<string> 
using namespace std; 

int main() 
{ 
ifstream inputFile, processLine; 
ofstream outputFile; 
string title, band; 
double price, total = 0; 

inputFile.open("orders.txt"); 
while (!inputFile.eof()) 
{ 
    getline(inputFile, title); 
    getline(inputFile, band); 
    inputFile >> price; 
    total += price; 

    cout << "Welcome to Megan McCracken's Online Music Store" << endl; 
    cout << "You have submitted the following order:" << endl; 
    cout << "***************************************************************************" << endl; 
    cout << "Title" << setw(46) << "Artist" << setw(24) << "Cost" << endl; 

    cout << title << setw(28) << band << setw(22) << fixed << setprecision(2) << price << endl; 
    cout << title << setw(30) << band<< setw(19) << fixed << setprecision(2) << price << endl; 
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl; 
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl; 
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl; 

    cout << "--------------------------------------------------------------------------" << endl; 
    cout << "Total Due:" << setw(75) << fixed << setprecision(2) << total << endl; 
    cout << "==========================================================================" << endl; 

} 


    /* getline(inputFile, title); 
    cout << left << title; 
    getline(inputFile, band); 
    cout << setw(23) << band; 
    inputFile >> price; 
    cout << setw(10) << fixed << setprecision(2) << price << endl; */ 






system("pause"); 
return 0; 

} 
+0

[ループ状態のeofが常に間違っている理由](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – kfsone

答えて

1

このコードには複数の問題があります。

while (!inputFile.eof()) 

This is always a bug。詳細についてはリンク先の記事を参照してください。

これは唯一の問題ではありません。

getline(inputFile, band); 
inputFile >> price; 

は、同じ入力ストリームにstd::getline()operator>>の両方を使用しないでください。 operator>>には、空白を扱う際に狭く制限されたセマンティクスがあります。ここで、operator>>は末尾の改行を消費しないので、次のループ反復では最初のgetline()がレールから外れることになります。

多くの場合、推奨されるいくつかのバンドエイドフィックスがありますが、この場合、最も簡単な解決策は、単にoperator>>を使用しないことです。

getline()を3回目に使用し、3行目をstd::stringに読み込みます。その後、それを使って独立したstd::istringstreamを構築し、それにoperator>>を使用します。問題が解決しました。

これを修正した後、いくつかの論理エラーを修正する必要があります。レポートの見出しはループの前に表示されるはずですが、ループ内で実行する必要があるのは、領収書の1行を表示し、合計を合計した後、ファイルの終了後に合計を表示することだけです。

+0

あなたは作品を修正しますすばらしいことですが、std :: istringstreamを使用できないと仮定した場合のオプションは何ですか。使用できるのは、getline関数とinputFile >>関数だけです。それを行う可能な方法はありますか? –

+0

これは合理的な前提ではありません。 'std :: istringstream'はC++ライブラリの一部です。存在し、うまく動作します。どこでも 'std :: getline'と' operator >> 'を見つけることができます。 –

関連する問題