Search code examples
c++classcoutflushdisplay

Why isn't half of my cout statement printing?


I'm printing a statement from classes using getter functions. The top 1.5 lines of my cout statement are not printing. I have tried flushing the stream, and I also copied and pasted the lines that are not printing outside the if statement, and it prints! I can't figure out what is going on. Here is the function:

// display all books out on loan
void displayBorrowed(vector<LibraryBook>& book)
{

    cout << "Books currently checked out: " << endl << endl;

    for(unsigned int i = 0; i < book.size(); i++)
    {
        //cout << "ID: " << book[i].getId_() << "  Title: " 
             //<< book[i].getTitle_() << endl << endl;

        if(book[i].getIsLoaned_() == true)
        { 
            std::cout.flush();
            cout << "ID: " << book[i].getId_() << "  Title: "
                 << book[i].getTitle_() << "  Author: " 
                 << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
                 << "  Year Published: " <<  book[i].getYearPubl_() << endl
                 << "Due Date: " << book[i].getDueMonth_() << "/" 
                 << book[i].getDueDay_() << "/" << book[i].getDueYear_()
                 << " Date Borrowed:  " << book[i].getBorrwdMonth_() << "/"
                 << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
                 << endl << "Checked out by: " << book[i].getBorrwFirst_()
                 << " " <<  book[i].getBorrwLast_() << endl << endl;
        }
    }
}

It displays this:

Books currently checked out:

  Author: Brendan Behan  Year Published: 1958
Due Date: 8/2/2017 Date Borrowed:  7/21/2017
Checked out by: Cassie Peterson

If the lines in the if statement are copied out of the if statement it displays normally:

ID: 78620 Title: Zhuan Falun

I tried changing the if statement to false to display all the books not loaned, and they all displayed the same except for the very last book (number 50 finally displayed the id # and the title. I am at a complete loss. What is going on?

It should look like this:

ID: 78620  Title:  Zhuan Falun Author: Brendan Behan  Year Published: 1958
Due Date: 8/2/2017 Date Borrowed:  7/21/2017
Checked out by: Cassie Peterson

(havent formatted display yet)

I just changed it to this where I have every single element that doesn't display in its own cout statement, and NONE of it displays!! What??! (up until author, where it started displaying before, I mean.)

    for(unsigned int i = 0; i < book.size(); i++)
    {
        if(book[i].getIsLoaned_() == true)
        { 
            std::cout.flush();
            cout << "ID: " ;
            cout << book[i].getId_();
            cout << "  Title: ";
            cout <<  book[i].getTitle_();
            cout << "  Author: " 
                 << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
                 << "  Year Published: " <<  book[i].getYearPubl_() << endl
                 << "Due Date: " << book[i].getDueMonth_() << "/" 
                 << book[i].getDueDay_() << "/" << book[i].getDueYear_()
                 << " Date Borrowed:  " << book[i].getBorrwdMonth_() << "/"
                 << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
                 << endl << "Checked out by: " << book[i].getBorrwFirst_()
                 << " " <<  book[i].getBorrwLast_() << endl << endl;
        }

It prints when I put an endl at the end of each element:

        if(book[i].getIsLoaned_() == true)
        { 
            std::cout.flush();
            cout << "ID: " << endl;
            cout << book[i].getId_() << endl;
            cout << "  Title: " << endl;
            cout <<  book[i].getTitle_() << endl;
            cout << "  Author: "  << endl;
            cout << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_() << endl;
            cout << "  Year Published: " <<  book[i].getYearPubl_() << endl;
            cout << "Due Date: " << book[i].getDueMonth_() << "/"  << endl;
            cout << book[i].getDueDay_() << "/" << book[i].getDueYear_() << endl;
            cout << " Date Borrowed:  " << book[i].getBorrwdMonth_() << "/" << endl;
            cout << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_() << endl;
            cout << endl << "Checked out by: " << book[i].getBorrwFirst_() << endl;
            cout << " " <<  book[i].getBorrwLast_() << endl << endl;
        }


Books currently checked out:

ID:
47492
  Title:
 Borstal Boy
  Author:
Brendan Behan
  Year Published: 1958
Due Date: 8/
2/2017
 Date Borrowed:  7/
21/2017

Checked out by: Cassie
 Peterson

Solution

  • My suggestion:

    1. Print each of the members in their own line.
    2. Find out which member is problematic.
    3. Dig deeper into the contents of the member to understand how it got there and fix it.

        if(book[i].getIsLoaned_() == true)
        { 
            std::cout.flush();
            std::cout << "ID: " << book[i].getId_() << std::endl
                      << "Title: " << book[i].getTitle_() << std::endl
                      << "Author First: " << book[i].getAuthorFirst_() << std::endl
                      << "Author Last:" << book[i].getAuthorLast_() << std::endl
                      << "Year Published: " <<  book[i].getYearPubl_() << std::endl
                      << "Due Date Month: " << book[i].getDueMonth_() << std::endl
                      << "Due Date Day: " << book[i].getDueDay_() << std::endl
                      << "Due Date Year: " << book[i].getDueYear_() << std::endl
                      << "Borrowed Month: " << book[i].getBorrwdMonth_() << std::endl
                      << "Borrowed Day: " << book[i].getBorrwdDay_() << std::endl
                      << "Borrowed Year: " book[i].getBorrwdYear_() << std::endl
                      << "Checked out by first: " << book[i].getBorrwFirst_() << std::endl
                      << "Checked out by last: " <<  book[i].getBorrwLast_() << std::endl
                      << std::endl;
        }
    }