0

I have a program that I am doing that is supposed to take lines from a text file and read them, line by line. My program works well for the first line, but stops and does not read the last three. This is the code I have so far:

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;
}

I have taken part of the code out because it is not needed, it is just using different if/elif loops to validate data.

The file I am attempting to read is:

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

When I execute my code, it does the first line perfectly but will not read the last three and I cannot figure out why. Any help would be awesome.

R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • 3
    Before you go any further down this path, please read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Oct 09 '18 at 04:03
  • 1
    Also `inputfile.close();` inside the file reading loop is a questionable choice. I would strongly consider moving this out of the loop. – user4581301 Oct 09 '18 at 04:04
  • 1
    You close the file and return 0 inside the first iteration of the loop. – Retired Ninja Oct 09 '18 at 04:15
  • You may want to visit [cppreference.com - std::basic_istream::ignore](https://en.cppreference.com/w/cpp/io/basic_istream/ignore) along with [cppreference.com - std::basic_istream](https://en.cppreference.com/w/cpp/io/basic_istream) – David C. Rankin Oct 09 '18 at 04:30

1 Answers1

-1
 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;

You have to close the file and return after the loop otherwise the program exits after one iteration.

birdfreeyahoo
  • 445
  • 4
  • 9
  • Just to clarify: you **don't** have to close the file; the destructor will do that. Similarly (yes, I know you just copied the original code, and that's where the flaw is), instead of creating a default-constructed stream object and then opening the file, just construct it with the file. That is, change `ifstream inputfile; inputfile.open("test.txt");` to `ifstream inputfile("test.txt");`. – Pete Becker Oct 09 '18 at 13:36