c++: Std::cout缓冲区错误?对字符串变量和字符串字面值使用std::cout导致输出混乱
C++: Std::cout buffer error? Jumbled output using std::cout on string variable and string literal
提前表示抱歉。我有一个项目,我正在和另外三个人一起开发一个计算机科学导论。经过几个小时的搜索和调整我的输出/函数/类后,我真的需要寻求一些帮助。
我们的程序是一个仓库购物者的俱乐部客户端/库存管理器,由客户端接口控制类'Members'组成,将在main中使用,其中包括几个数据类向量(俱乐部成员类:'Regular'(基)和'Executive'(派生),以及项目类:'Item_Info'(派生)用于购买信息和' item '(基)用于库存信息)。定义如下:
class Members{
friend class RegularMember ;
friend class ExecutiveMember ;
friend class Item ; // unnecessary? should use access functions
// all three may be unneeded
private:
vector <RegularMember*> memberList ; // one member list
vector<Item_Info> item_sales ;
vector<Item> inventory ;
ifstream inFile ;
vector<string> strFiles ;
我们使用基类指针作为'memberList' vector类型来允许多态性,但是为了可读性和功能性,我们选择将'inventory'和'item_sales'分开(Item(base) &Item_Info(派生)有唯一的[stock/sold]Quantity成员变量,使用方式不同,这是最有意义的)。
进入实际错误,我们在程序运行时有两种形式的输入:成员列表文件,包含与'memberList'相关的所有信息;以及销售文件的日期x,其中有5个,包含与'inventory' &'item_sales'以及每个购买的memberID,将每个购买连接到一个成员。我们有一个函数用于读取成员输入文件并构建'memberList',还有一个函数用于读取销售日期文件,并在库存为空时构建库存。我已经能够清除所有的内存泄漏,条件跳转,未初始化的值,和其他容易修复的问题与valgrind。
然而,现在我的输入函数成功地通过它的逻辑和输出每个库存添加,我有一个非常奇怪的问题。当我尝试简单地输出一个字符串变量与字符串字面量连接时:
std::cout << itemTitle << " inventory'dn" ;
我在控制台输出中得到:
" inventory'd"+[whatever part of the string exceeds 13 characters (the length of my string literal.)]
本质上,如果itemTitle是"Banana",你将看不到任何输出,除了"inventory'd!",但如果它是itemTitle像"Panasonic宽屏",你将看到"inventory'd! screen"。我已经用尽了使用endl的所有可能的格式化排列,因为我的第一个猜测是这是某种流缓冲区问题,但我仍然不确定发生了什么。我还尝试将输出移动到itemTitle传递给其各自的对象并使用getter成员函数推入其向量后执行,但无济于事。输出my getName函数的返回结果与输出itemTitle变量的结果相同。
如果有人想要更多关于输入函数的上下文,它在85行左右很长,但如果有用的话:
void Members::inputDayOfSales(int day_i) { // input day of sales file
int j, i = 0 ;
int m, d, y, quantity, tmpint ;
double memberID, price ;
string itemTitle, day = to_string(day_i), file = "day.txt" ;
file.insert(size_t(3),day) ;
inFile.open(file) ;
for(int i = 0 ; i < memberList.size() ; ++i) //valgrind errors?
memberList[i]->setSpentToday(0,day_i) ;
while (inFile >> m) {
i = 0 ;
inFile.get() ;
inFile >> d ;
inFile.get() ;
inFile >> y ;
inFile >> memberID ;
cout << endl << memberID << endl ;
inFile.ignore(100, 'n') ;
getline(inFile, itemTitle) ;
inFile >> price ;
inFile >> quantity ;
cout << "Grabbed file info blockn" ; // grabs all the data from file
while(1)
{
if(i < memberList.size())
{
if(memberList[i]->getInfo().memberID == memberID)
{
break ; // once found, break from loop
}
++i ;
}
}
cout << memberList[i]->getInfo().memberID << endl << endl ;
tmpint = memberList[i]->getSpentToday(day_i) ;
memberList[i]->setSpentToday((price * quantity), day_i) ;
memberList[i]->addtoTotalSpent(price * quantity) ;
j = 0 ;
if(!inventory.empty()) {
while(j < inventory.size()) {
if(inventory[j].getName() == itemTitle
|| j == inventory.size()-1) {
break ;
}
++j ;
}
if(inventory[j].getName() == itemTitle) {
cout << endl ;
cout << "Already inventory'd!n" ;
Item_Info newsale ;
Date saleDate(m,d,y) ;
newsale.setPurchaseDate(saleDate) ;
newsale.setRevenue(price*quantity) ;
newsale.setSoldQuantity(quantity) ;
item_sales.push_back(newsale) ;
}
else {
newinv_l:
Item newinv ;
Item_Info newsale ;
Date saleDate(m,d,y) ;
newinv.setName(itemTitle) ;
newinv.setPrice(price) ;
newsale.setPurchaseDate(saleDate) ;
newsale.setRevenue(price*quantity) ;
newsale.setSoldQuantity(quantity) ;
item_sales.push_back(newsale) ;
inventory.push_back(newinv) ;
cout << "n" ;
// all three are having the same issue:
//cout << itemTitle ;
//cout << newinv.getName() ;
//cout << inventory.back().getName() ;
cout << " inventory'd!" << endl ;
}
}
else if(inventory.empty()) goto newinv_l ;
i = 0 ;
}
inFile.ignore(100, 'n') ;
inFile.close() ;
}
将评论中的猜测转移到答案中:
在itemTitle
字符串的末尾有一个返回('r'
)字符。在打印此命令时,下一个字符将被输出的点被移回控制台的左侧。输出时,下一个字符将覆盖打印的第一个字符。
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么我的 cout 在输出中没有显示字符串?
- 使用 cout 运算符重载字符串
- C++ - 如何从上到下制作 4x4 矩阵和 cout 列(需要解密字符串)
- C++ 不能在cout中使用向量和字符串文字
- 使用字符串流加速 std::cout 日志记录
- C++ cout 不接受字符串或带 + 的字符串
- cout 切断 for 循环中的字符串开头
- 使用带有 std::cout 的单引号打印字符串实际上打印了数字
- 为什么cout输出内存地址不是字符串?
- 为什么我的 C++ 代码不能显示 cout 字符串
- 在COUT字符串期间,在控制台上闪烁光标
- C++ cout 字符串不起作用.请指教
- cout 字符串和向量在同一行中
- 如何更改对象的 cout 字符串
- 在 std::cout 字符串上崩溃
- cout 字符串和 c_str 在 C++ 中给出不同的值
- 在c++中使用字符串(搜索字符串,分割字符串,cout<<字符串)
- Flex cout字符串崩溃
- 三元运算符中的 Cout 字符串和 int