2017-10-06 9 views
1

このプログラムは、ファイルから4〜5行の情報を読み取ることになっています。私はプログラムの最初の行を読み込み、さまざまなアルゴリズムで処理することができますが、次の行をどのようにループして、ファイルの最後まで何度も処理する方法がわかりません。読んでいただきありがとうございます。ここに全体のプログラムがあり、テキストはファイルの最後から読み込まれます。ファイルから次の行を読み取る

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

int main() 
{ 
    ifstream inputFile; 
    ofstream invoicefile; 
    string name, author, isbn, customerid, filename, fictionoutput, genreoutput; 
    char booktype, genre; 
    bool fictionvalue; 
    double initial_total, tax_price, subtotal, totalprice, price; 
    int fee, quantity; 
    const double tax(0.07); 

    cout << "Enter name of file.\n"; 
    cin >> filename; 
    cout << "Opening file \n"; 
    inputFile.open(filename); 

    if (inputFile.is_open()) { 
     inputFile >> customerid >> name >> author >> isbn >> price >> quantity >> booktype >> genre; 

     //QUANTITY FEE CODING BLOCK 
     if (quantity > 50) { 
      fee = 50; 
     } 
     else if (quantity >= 15 && quantity <= 19) { 
      fee = 40; 
     } 
     else if (quantity >= 10 && quantity <= 14) { 
      fee = 30; 
     } 
     else if (quantity >= 5 && quantity <= 10) { 
      fee = 20; 
     } 
     else if (quantity < 5) { 
      fee = 10; 
     } 

     //BOOKTYPE CODING BLOCK (FICTION or NON-F) 
     if (booktype == 'F') { 
      fictionvalue = true; 
     } 
     else if (booktype == 'N') { 
      fictionvalue = false; 
     } 
     else { 
      cout << "INVALID"; 
     } 
     //BOOKTYPE INTO STRING OUTPUT 
     if (fictionvalue = true) { 
      fictionoutput = "Fiction"; 
     } 
     else if (fictionvalue = false) { 
      fictionoutput = "Non-Fiction"; 
     } 

     //GENRE TYPE INTO STRING OUTPUT 
     if (genre == 'R') { 
      genreoutput = "Romance"; 
     } 
     else if (genre == 'D') { 
      genreoutput = "Drama"; 
     } 
     else if (genre = 'M') { 
      genreoutput = 'M'; 
     } 
     else { 
      cout << "Invalid entry\n"; 
     } 

     //NO FEE EXCEPTION 
     if (booktype == 'N' && genre == 'R') { 
      fee = 0; 
     } 


     //CALCULATION OF PRICE + TAX CODING BLOCK 
     initial_total = (price*quantity); 
     tax_price = (initial_total * tax); 
     subtotal = (initial_total + tax_price); 
     totalprice = (subtotal + fee); 



     //OUTPUT TO FILE/CONSOLE CODING BLOCK 
     cout << "-----------------------------------------" << endl; 
     cout << "Order Invoice" << endl; 
     cout << "Customer ID: " << customerid << endl; 
     cout << name << " " << author << " " << fictionoutput << " " << genreoutput << " " << quantity << "@" << price << "Subtotal: " << endl; //add subtotal price 
     //cout << "Total book sales: " << 
     cout << "Tax: " << tax_price << endl; 
     cout << "Subtotal: " << subtotal << endl; 
     cout << "Fee: " << fee << endl; 
     cout << "Total Price: " << totalprice << endl; 

     cout << "-----------------------------------------" << endl; 
     system("pause"); 
    } 

} 

1234 Dog_Strategy Henry_Moreno 3-598-21500-2 12.99 5 N M 
6789 Companion_Kicked_Me_Out Lorraine_Johnson 3-598-21599-1 24.99 3 F R 
3444 Mime_On_My Journey Kristy_Wahl 3-699-21500-8 6.75 10 N D 
4455 Damaged_By_The_Joke Henry_Christopher 3-598-21500-2 12.99 4 N R 
+3

ようこそスタックオーバーフロー。タイトルのタグ情報を繰り返す必要はありません。また、ヘルプを叫ぶ必要はありません。なぜなら、助けが必要ない場合は、ここに投稿しないことになるからです。代わりに、より良いタイトルを書く努力を費やしてください。もっと良いことに、C++でファイルを1行ずつ読むことについては数十件あるので、ここで既存の投稿を検索してください。 –

答えて

1

テキストサンプルは、たぶん、このようにループを使用してみてください:

// Create an empty string 
std::string line; 

// Start a loop that will get a line from the file and input it in our string 
// this loop will keep going until the getline fails, i.e. end of file. 
while (std::getline(fileName, line)) 
{ 
CODE 
} 
+0

文字列やその他の文字列を使用して、文字列 'line'から情報を取得してください – kewak

+0

本当に助けてくれてありがとうございました...唯一の問題は、最初の行を完全にスキップして、後に行をインポートしたようです。なぜこれが分かっていますか?あなたのお返事ありがとうございました! (getline(inputFile、line)) –

-2

あなたはプログラムがファイル

の終わりを見るまで実行されますwhileループを置くことができます

いつも忘れることはありません開いたファイルを閉じます。

+0

これはお勧めしません。 https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik

+0

あなたは**常に*読んでから*成功したかどうかを確認する必要があります。 "EOF"にあることの検出によるループ制御は、通常、ファイル内の最後の文字である可能性が高い改行文字で以前の読み込みを停止するため、*動作しません。これ以上の文字はありませんが、stram doesnそれがファイルの最後にあるとは思わない。また、 'std :: ifstream'と' std :: ofstream'のデストラクタが自動的にファイルを閉じます。 –

関連する問題