Astrongitch
Astrongitch

Reputation: 1

C++ Difficulty with File Input Loop?

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!

Upvotes: 0

Views: 75

Answers (1)

David
David

Reputation: 28178

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

Upvotes: 1

Related Questions