Thread: ifstream with getline and inputfile.eof() is not working.

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    14

    ifstream with getline and inputfile.eof() is not working.

    This is not a homework but just a self study.
    I am trying to read the information from inputFile and then loop through the data and display it. However; the below code falls into infinite loop. What am I doing wrong?
    Code:
    #include <iostream>#include <fstream>
    
    
    using namespace std;
    
    
    int main()
    {
        ofstream outputFile;
        outputFile.open("Store Sales.txt");
        const int MAX_STORE = 5;
        double sales;
        int asteriks;
        string storeName;
        // Loop through each store and get the sales value
        for(int storeCounter = 1 ; storeCounter <= MAX_STORE ; storeCounter++)
        {
            cout << "Enter the sales for store #" << storeCounter << ": " << '\n';
            cin >> sales;
            outputFile << "Store " << storeCounter << '\n';
            outputFile << sales << '\n';
        }
        // Close the file
        outputFile.close();
    
    
        // Open the same file and show the asterisks graph bar
        ifstream inputFile;
        inputFile.open("Store Sales.txt");
    
    
        // Test whether the file exists
        if(inputFile)
        {
            cout  << "SALES BAR CHART" << endl;
            cout << "(Each *= $100)" << endl;
            while(!inputFile.eof())
            {
                getline(inputFile,storeName);
                cout << storeName << ": ";
                inputFile >> sales;
                asteriks = (sales / 100);
                for(int i = 1; i<= asteriks; i++)
                {
                    cout << "*";
                }
                cout << endl;
            }
        }
        else
        {
            cout << "Cannot open the file!" << endl;
            return 0;
        }
        // Close the file
        inputFile.close();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    .eof() returns true only when you try to read past the end of the file.

    Try this instead:

    Code:
    while(getline(inputFile,storeName);)
    {
        ...
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also please post a sample of your input file.

    Jim

  4. #4
    Guest
    Guest
    I think the problem is that inputFile >> sales; on line 40, after having read your sales value, doesn't skip past the newline character in the file. The next call to std::getline then consumes that newline and the following inputFile >> sales; reads non numerical data it can't deal with. If you make sales of type char, the loop should terminate.

    To fix this, you could call inputFile.get() after reading into sales. I'm not sure if this is the best practice though.

    Generally speaking, it's better to store associated sales data on the same line, separated by spaces and use std::getline as Elkvis suggested. You can then safely pry apart the written string (or stringstream) containing the whole line.
    Last edited by Guest; 02-23-2016 at 03:10 PM.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    This is basically it.
    Store 1
    100
    Store 2
    200

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    Also please post a sample of your input file.

    Jim
    Text file is this
    Store 1
    100
    Store 2
    200

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay, so as already suggested the problem is that your input operations are failing so you enter an endless loop because you're only checking for eof() not any other file read problem. So to solve the problem you could do something like:

    Code:
            while(getline(inputFile >> ws, storeName))  // The ws manipulator will force getline() to skip all leading whitespace.
            {
                cout << storeName << ": ";
                inputFile >> sales;
                asteriks = (sales / 100);
                for(int i = 1; i<= asteriks; i++)
                {
                    cout << "*";
                }
                cout << endl;
            }
    Jim

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    Okay, so as already suggested the problem is that your input operations are failing so you enter an endless loop because you're only checking for eof() not any other file read problem. So to solve the problem you could do something like:

    Code:
            while(getline(inputFile >> ws, storeName))  // The ws manipulator will force getline() to skip all leading whitespace.
            {
                cout << storeName << ": ";
                inputFile >> sales;
                asteriks = (sales / 100);
                for(int i = 1; i<= asteriks; i++)
                {
                    cout << "*";
                }
                cout << endl;
            }
    Jim
    Perfect. This works.
    Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why would ifstream::getline hang and not return?
    By Programmer_P in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2010, 02:34 PM
  2. ifstream with getline?
    By DigiAcid in forum C++ Programming
    Replies: 13
    Last Post: 03-08-2009, 08:14 AM
  3. problem with string class, ifstream, and getline
    By deathbob in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2005, 11:20 AM
  4. ifstream getline
    By jimboob in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2004, 10:37 AM
  5. getline() and ifstream
    By quizkiwi in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2004, 01:44 PM

Tags for this Thread