0

I'm a very novice programmer and I'm having trouble with File input looping.

I'm reading from a list of books and their associated titles, cover types, pages, and weights. I've currently spent over an hour trying to figure out how to write this so that the loop continues on to the next grouping of data(title followed by cover type followed by page count, and ending on weight).

The text file contains the following:

The Human Use of Human Beings
0
200   
8.0    
Utopia 
0    
176    
4.8    
Hackers    
0    
520   
22.4    
The Information    
1    
544    
33.6   
Sterling's Gold    
1    
176    
8.0

Here is my main.cpp:

using namespace std;

int main()
{
    const string FILENAME("books.txt");

    ifstream input(FILENAME);

    if( input.good() )
    {
        while( !input.eof() )
        {
            string name;
            int type;
            int pages;
            float ounces;
            getline( input, name );
            input >> type >> pages >> ounces;
            input.ignore(INT_MAX, '\n');  

            Book newBook(name, static_cast<Type>(type), pages, ounces);

            cout << newBook.formatReportLine() << endl; 
        }
    }
    else
    {
        cout << "File not found: " << FILENAME << endl;
    }

    return 0;
}

Here is my book.cpp file:

//Constructor
Book::Book( const string& name, Type type, int pages, float ounces )
{
    bName = static_cast<string>(name);
    bType = type;
    bPages = pages;
    bOunces = ounces;
}

//Default Constructor
Book::Book() {
    bName = "";
    bType = UNKNOWN;
    bPages = 0;
    bOunces = 0.0f;
}

float Book::getWeightLbs()
{
    const float OUNCES = 16.0f;
    return bOunces / OUNCES;
}

string Book::getTypeName()
{
    return TYPE_WORDS[bType];
}

string Book::formatReportLine() 
{
    stringstream reportLine;
    reportLine << Book::getName() << setw(10) << "| Type: " << Book::getTypeName() << setw(10) << "Pages: " << Book::getPages()<< setw(10) 
    << "Weight: " << Book::getWeightLbs(); 

    return reportLine.str();
}

And finally book.h

using namespace std;

enum Type
{
    UNKNOWN = -1,
   PAPERBACK,
    HARDBACK
};

const string TYPE_WORDS[] = { "Paperback", "Hardback" };

class Book
{
public:
    Book();
    //constructor
    Book( const string& name, Type type, int pages, float ounces );
    //destructor
    ~Book(){};

    string formatReportLine();  
    float getWeightLbs();  
    string getTypeName();  

    //accessors
    string getName(){ return bName; };
    Type getType(){ return bType; };
    int getPages(){ return bPages; };
    float getOunces(){ return bOunces; };

private:
    string bName;  //name of the book
    Type bType;  //the type of book 
    int bPages;  //how many pages the book contains
    float bOunces;  //how much the book weighs in ounces
};

Currently it prints out the first line correctly, then infinitely loops the first book's cover type, pages, and weight without the title. I've been spending a long time trying to figure this one out. Any help would be immensely appreciated, Thank you!

  • 1
    http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – Barmar Jan 27 '15 at 04:25
  • Or for the C++ ilk , [this question](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Jan 27 '15 at 04:32
  • Alright thanks. So there's reason to believe it's the loop itself as opposed to any other part of the code? – Astrongitch Jan 27 '15 at 04:37
  • I ran this on vs2013 with only the loop changes and it runs fine on my computer. You should include your #include statements in your code, b/c the pages may not be linking correctly. – TriHard8 Jan 27 '15 at 05:07

1 Answers1

1

Change your loop to:

string name;
int type;
int pages;
float ounces;

while(getline(input, name) >> type >> pages >> ounces)
{
     // do something with name, type, pages, and ounces
}

As was already linked, see "while( !feof( file ) )" is always wrong

David
  • 27,652
  • 18
  • 89
  • 138