1

So the program is supposed to read from the file, which has 4-5 lines of information on it. I can read the first line into the program, and process it through the various algorithms, but I'm not sure how to loop the next line into it and process it as well, again and again until the end of the file. Thank you very much for reading and all input is appreciated. Here is the entire program, with the text read in from file at the bottom.

#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");
    }

}

TEXT 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
Ken White
  • 117,855
  • 13
  • 197
  • 405
  • 3
    Welcome to Stack Overflow. It is not necessary to repeat tag information in the title. It is also not necessary to shout HELP, because if you didn't need help you would not be posting here. Instead, spend the effort writing a better title. Better yet, do some searching here for existing posts, because there are dozens of them about reading files line by line in C++. – Ken White Oct 06 '17 at 02:47

2 Answers2

1

Maybe try using a loop like this:

// 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
}
kewak
  • 26
  • 1
  • then use a string stream or something to get your information from the string 'line' – kewak Oct 06 '17 at 02:59
  • Thanks a lot that definitely helped...the only problem is it seems to have skipped the first line altogether and only imported the lines after it. Do you know why this is? Thanks a lot for your response! ('heres the code I entered before the rest) while(getline(inputFile, line)) – Space Cowboy Oct 06 '17 at 15:29
-2

you can put a while loop that will run until the program saw the end of file

while(!EOF)
{your code here}

and always dont forget to close the file you opened

  • This is not recommended. https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik Oct 06 '17 at 03:00
  • You **always** need to check for successful read *after* an attempt to read. Loop control via detection of being at "EOF" does *not* work, e.g., because typically you stop the previous read at the newline character which is likely to be the last character in a file: there are no more characters but the stram doesn't think it is at the end of the file, yet. Also, the destructors of `std::ifstream` and `std::ofstream` automatically close the file. – Dietmar Kühl Oct 06 '17 at 03:59