为什么我的 cout 语句没有打印一半?

Why isn't half of my cout statement printing?

我正在使用 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语句中复制出来的,则正常显示:

编号: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. 在各自的行中打印每个成员。
  2. 找出哪个成员有问题。
  3. 深入了解成员的内容以了解它是如何到达那里并修复它的。

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

/* 有时“half cout statement not printing problem”可能是由于使用了悬空指针或危险指针造成的。在下面的代码中,所有出现的 cout 语句在悬挂指针打印语句之后都没有打印任何内容。 如果我们删除悬挂指针的 cout 语句,那么一切都会好起来的。

代码: // 悬空指针很危险,因为它可以保存不需要的地址。 */

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a =5;
    char b= 'a';
    int *p = &a;
    char *p1 =&b;
    int *p3;  // p3 is dangling pointer

    cout<<"size of p "<<sizeof(p)<<endl; // o/p is 8, bcoz x64 bit compiler 
                                           gives 8byte and x32 gives 4 byte.
    cout<<"size of p1 "<<sizeof(p1)<<endl;

    cout<<"size of p3 "<<sizeof(p3)<<endl;
    cout<<"address of p3 is "<<&p3<<endl;

    cout<< "value at p3 is "<<*p3<<endl; //***dangling or dangerous pointer.***
   
    cout<<"hello my name is rahul ";
    cout<<a<<endl;
    cout<<&a<<endl;
    //cout<<*a<<endl;  // shows error (invalid type argument)
    cout<<p<<endl;    // shows base address
    cout<<&p<<endl;  // shows pointers address
    cout<<*p<<endl;  
    cout<<"this is an end"<<endl; 
 
  return 0;
}