为什么我的一半没有打印出来?
Why isn't half of my cout statement printing?
我正在使用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
我的建议:
- 在自己的行中打印每个成员。
- 找出哪个成员有问题。
- 更深入地研究成员的内容,以了解其如何到达那里并修复它。
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;
}
相关文章:
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么我的字符串变量没有打印完整的字符串?
- 我的问题是关于C++中数字和序列的重复以及如何打印它们?
- 为什么我的打印功能不起作用?链表
- 在我的变量打印C++后无法获得 $ 打印符号
- 如何打印我在地图中使用的数组?
- 我的链表没有按预期打印出来?
- 只有我的"链接列表"中的第一个节点正在打印
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 打印我的字符串阵列时,会遇到细分错误
- Cout不会打印我的字符串
- 如何在C++的下一行打印我的名字
- 我不能打印我的书的名字,但可以打印其他所有的东西
- 当我尝试通过我的函数打印我的列表时出现分段错误
- 打印我的Get MAC地址函数的std::字符串时出现意外输出
- 为什么此代码不打印我的数组?
- 链表需要帮助无法打印我的数据.想要添加功能.在 C - C++ 中
- 在C++中以相反的顺序打印我的链接列表