2016-04-22 5 views
0

私のクラス用のプログラムをビルドしていますが、.txtファイルからデータをインポートする必要があり、項目名/価格はsaltes taxと総計を計算するために使用されます。私の先生はこれを例に挙げていますが、実行することはできません。C++ fstreamビルディングですが実行されていません

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

int main() 
{ // Beginning of main function 

string name; 
ifstream data_in; 
ofstream data_out; 

int cnt=0; 
int number; 

struct Item 
{ 
    int item_n; 
    char disc[50]; 
    float price; 
}; 

Item store[999]; 

data_in.open("cost.txt"); 
data_out.open("file_out.txt"); 

while(!data_in.eof()) 
{ 
    //cout << "Enter in the item number: "; 
    data_in >> store[cnt].item_n; 
    //cout << "Enter in the description for item number " << store[cnt].item_n << ": "; 
    data_in >> store[cnt].disc; 
    //cout << "Enter in the price for the " << store[cnt].disc << ": $"; 
    data_in >> store[cnt].price; 
    cnt++; 
} 
cout << endl << endl; 

number = cnt; 
for (cnt=0; cnt<number; cnt++) 
{ 
    name = store[cnt].disc; 
    cout << setw(5) << store[cnt].item_n << " " << store[cnt].disc << setw(16-name.length()) << "$" << setw(9) << store[cnt].price << endl; 
} 

for (cnt=0; cnt<number; cnt++) 
{ 
    name = store[cnt].disc; 
    data_out << setw(5) << store[cnt].item_n << " " << store[cnt].disc << setw(16-name.length()) << "$" << setw(9) << store[cnt].price << endl; 
} 


return 0; 
} 

そして、これがcost.txtファイルの情報

ブックス45.01 ペン21.03 鉛筆10.90 帽子50.00 キャップ800.00 食品1.00

+1

入力ファイルと出力ファイルが実際に開いていることを確認しましたか? – Stephen

+1

より正確にお願いします。 "走らせることができない"というのは無駄です - それは何ですか? – Unimportant

+0

デバッガを使用したときに、どのステートメントが問題を引き起こしていましたか? –

答えて

0

次の変更を試してみてくださいで、残りあなたのコードがうまくいくようです:

data_in.open("cost.txt"); 
data_out.open("file_out.txt"); 
if (!data_in.is_open() || !data_out.is_open()) //Test if files opened correctly... 
{ 
    cout << "Failed to open a file!\n"; 
    return 1; 
} 

float SalesTotal = 0; 
while(true) 
{ 
    if (!(data_in >> store[cnt].disc)) 
    { 
     //Failed to read first element in this record - could be eof or format error 
     if (data_in.eof()) 
      break;   //eof - all records read. 

     cout << "Format error first field\n"; 
     return 1;   //format error on first field. 
    } 

    if (!(data_in >> store[cnt].price)) 
    { 
     cout << "Format error second field\n"; 
     return 2;   //format error on second field. 
    } 

    store[cnt].item_n = cnt + 1; //Item number is not a field in your file, use the counter... 

    SalesTotal += store[cnt].price; 
    cnt++; 
} 

if (!cnt) 
{ 
    cout << "No records read\n"; 
    return 3; //No valid records read. 
} 

float GrandTotal = ((SalesTotal/100) * 6) + SalesTotal; 
cout << "Sales total: " << SalesTotal << " Grand total:" << GrandTotal << "\n"; 
+0

それはうまくいった。助けてくれてありがとう! – Bnstates

+0

もう1つのことで私を助けることができると思いますか?プログラムでは、すべての商品の売上合計を計算し、6%の税金と総額を加算する必要があります。どのような式を使用できますか?私は標準のtotal_sales = total_sales + store [cnt] .priceを試しました。これは他のプロジェクトでやった方法ですが、ストア[cnt] .priceで動作しません。 – Bnstates

+0

@Bnstates:私の答えを更新しました。 – Unimportant

0

あなたが書いたコードは、各Itemの3つのもの:アイテム番号、説明、および価格を読み取ります。

あなたが表示したサンプルデータファイルには、各アイテムについて2つのもの、つまり説明と見えるものと価格が含まれています。

予想されるデータ形式が入力ファイルの内容と一致しません。このコードはそのまま動作しません。どちらか一方が間違っています。コメントに記載されているように、コードの他のすべての問題にプラス。

関連する問題