2017-10-06 136 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

歡迎使用堆棧溢出。標題中不需要重複標籤信息。這也沒有必要喊HELP,因爲如果你不需要幫助,你就不會在這裏發帖。相反,花費精力寫出更好的標題。更好的是,在這裏搜索一下現有的帖子,因爲有很多關於在C++中逐行閱讀文件的文章。 –

回答

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

然後使用字符串流或其他東西從字符串'行' – kewak

+0

得到您的信息非常感謝,這絕對有幫助...唯一的問題是它似乎已經完全跳過第一行,並只在其後導入行。你知道這是爲什麼嗎?非常感謝您的回覆! ('繼承我之前輸入的代碼) while(getline(inputFile,line)) –

-2

,你可以把一個while循環將運行,直到程序看到文件

結束
while(!EOF) 
{your code here} 

永遠不要忘記關閉您打開的文件

+0

這是不推薦的。 https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik

+0

你**總是**需要在嘗試閱讀後檢查成功閱讀*。通過檢測在「EOF」處的循環控制確實不工作,例如,因爲通常你停止在可能是文件中最後一個字符的換行符處的前一次讀取:沒有更多字符,但是stram不會不認爲它是在文件的末尾,但。另外,'std :: ifstream'和'std :: ofstream'的析構函數會自動關閉文件。 –