Пока (! Inputfile.eof ()) читает только первую строку? - PullRequest
0 голосов
/ 09 октября 2018

У меня есть программа, которую я делаю, которая должна брать строки из текстового файла и читать их, строка за строкой.Моя программа хорошо работает для первой строки, но останавливается и не читает последние три.Это код, который у меня есть:

ifstream inputfile;
ofstream outputfile;
inputfile.open("test.txt");
outputfile.open("invoice.txt");

while (!inputfile.eof())

{

    inputfile >> cust_id;
    inputfile >> title;
    inputfile >> author;
    inputfile >> isbn;
    inputfile >> isbn2;
    inputfile >> isbn3;
    inputfile >> isbn4;
    inputfile >> price;
    inputfile >> quanity;
    inputfile >> type;
    inputfile >> genre;


    outputfile << "____________________________________________________________________________________________________________" << endl;
    outputfile << "Invoice" << endl;
    outputfile << " " << endl;
    outputfile << "Customer ID: " << cust_id << endl;
    outputfile << title << "      " << fiction_type << "     " << genre_type << "        " << quanity << "@" << price << "      " << "subtotal: " << subtotal << endl;
    outputfile << " " << endl;
    outputfile << "Total book sales: " << subtotal << endl;
    outputfile << "Tax: " << totaltax << endl;
    outputfile << "Subtotal: " << subtotal_withtax << endl;
    outputfile << "Fee: " << fee << endl;
    outputfile << "Total: " << total_price << endl;

    cout << "something" << endl;
    inputfile.close();
    return 0;
}

Я вынул часть кода, потому что он не нужен, он просто использует разные циклы if / elif для проверки данных.

Файл, который я пытаюсь прочитать:

234 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

Когда я выполняю свой код, он отлично выполняет первую строку, но не читает последние три, и я не могу понять, почему.Любая помощь будет потрясающей.

1 Ответ

0 голосов
/ 09 октября 2018
 ifstream inputfile;
ofstream outputfile;
inputfile.open("test.txt");
outputfile.open("invoice.txt");

while (!inputfile.eof())

{

    inputfile >> cust_id;
    inputfile >> title;
    inputfile >> author;
    inputfile >> isbn;
    inputfile >> isbn2;
    inputfile >> isbn3;
    inputfile >> isbn4;
    inputfile >> price;
    inputfile >> quanity;
    inputfile >> type;
    inputfile >> genre;


    outputfile << "____________________________________________________________________________________________________________" << endl;
    outputfile << "Invoice" << endl;
    outputfile << " " << endl;
    outputfile << "Customer ID: " << cust_id << endl;
    outputfile << title << "      " << fiction_type << "     " << genre_type << "        " << quanity << "@" << price << "      " << "subtotal: " << subtotal << endl;
    outputfile << " " << endl;
    outputfile << "Total book sales: " << subtotal << endl;
    outputfile << "Tax: " << totaltax << endl;
    outputfile << "Subtotal: " << subtotal_withtax << endl;
    outputfile << "Fee: " << fee << endl;
    outputfile << "Total: " << total_price << endl;

    cout << "something" << endl;

}

inputfile.close();
return 0;

Вы должны закрыть файл и вернуться после цикла, в противном случае программа завершится после одной итерации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...