2017-10-06 3 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"); 
    } 

} 

텍스트 SAMPLE

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

어쩌면이 같은 루프를 사용해보십시오 :

// 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

많은 도움을 주셔서 감사합니다. 유일한 문제는 첫 번째 줄을 모두 건너 뛰고 그 뒤의 줄만 가져온 것 같습니다. 이것이 왜 있는지 아십니까? 답변 해 주셔서 감사합니다. ('나머지는 전에 입력 한 코드를 사용합니다.) 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 doesn 아직 파일의 끝 부분에 있다고 생각하지 않습니다. 또한'std :: ifstream'과'std :: ofstream'의 소멸자가 자동으로 파일을 닫습니다. –

관련 문제