为什么我的cout声明中有一半没有打印?

时间:2017-04-19 06:03:26

标签: c++ class cout flush display

我正在使用getter函数从类中打印语句。我的cout语句的前1.5行不打印。我试过刷新流,我也复制并粘贴了if语句之外没有打​​印的行,然后打印出来!我无法弄清楚发生了什么。这是功能:

// 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;
        }
    }
}

显示:

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语句中的行被复制出if语句,它将正常显示:

ID:78620标题:转法轮

我尝试将if语句更改为false以显示所有未借出的书籍,并且除了最后一本书之外它们都显示相同(数字50最终显示了id#和标题。我完全失去了。发生了什么事?

它应该是这样的:

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

(尚未格式化显示)

我刚刚把它改成了这里,我的每一个元素都没有在它自己的cout语句中显示,并且它的NONE显示!!什么??! (直到作者,之前它开始显示,我的意思是。)

    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

1 个答案:

答案 0 :(得分:1)

我的建议:

  1. 以自己的方式打印每个成员。
  2. 找出哪个会员有问题。
  3. 深入了解会员的内容,了解其如何到达并修复它。
  4.     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;
        }
    }