为什么我的一半没有打印出来?

Why isn't half of my cout statement printing?

本文关键字:打印 我的 一半 为什么      更新时间:2023-10-16

我正在使用Getter函数从类中打印一个语句。我的代表语句的前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标题:Zhuan Falun

我尝试将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

(尚未格式化显示(

我只是将其更改为此,其中我的每个元素在其自己的代表语句中都没有显示,并且没有显示!什么??!(直到作者,我的意思是之前开始显示的地方。(

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

/*有时&quot&quot; cout语句不打印问题&quot;可能由于使用悬挂或危险的指针而发生。在下面的代码中,悬挂的指针打印说明之后的所有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;
}