WHILE循环和数据输入
WHILE loop and data entry
我以前尝试过使用for
循环来放入数据,但问题太大了。所以我尝试使用while
循环,它可以工作,但当我尝试调试它时,它继续将-858993460
放入每个插槽。.dat
文件位于正确的位置并打开。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct record
{
int item_id;
string item_type;
float item_price;
int num_stock;
string item_title;
string item_author;
int year_published;
};
void read_all_records(record records[], int &valid_entries);
int num_inventory_of_type(record records[], string type, int &valid_entries);
const int max_array = 100;
int main()
{
int valid_entries = 0;
record records[max_array];
read_all_records(records, valid_entries);
cout << "Stock Report" << endl;
cout << "------------" << endl;
int book = num_inventory_of_type(records, "book", valid_entries);
cout << "Book's In Stock: " << book << endl;
int cd = num_inventory_of_type(records, "cd", valid_entries);
cout << "CD's In Stock: " << cd << endl;
int dvd = num_inventory_of_type(records, "dvd", valid_entries);
cout << "DVD's In Stock: " << dvd << endl;
return 0;
}
void read_all_records(record records[], int &valid_entries)
{
ifstream invfile;
invfile.open("inventory.dat");
if (!invfile.is_open())
{
cout<<"file open failed";
exit(1);
}
while(invfile.good() && valid_entries < max_array)
{
invfile >> records[valid_entries].item_id >> records[valid_entries].item_type
>> records[valid_entries].item_price >> records[valid_entries].num_stock
>> records[valid_entries].item_title >> records[valid_entries].item_author
>> records[valid_entries].year_published;
if(!invfile.good())
break;
valid_entries++;
}
invfile.close();
}
int num_inventory_of_type(record records[], string type, int &valid_entries)
{
int count = 0;
int holder = 0;
for (int count = 0; count<valid_entries; count++);
{
if (records[count].item_type == type)
{
holder+=records[count].num_stock;
}
}
return holder;
}
.dat文件是
123456
book
69.99
16
Problem_Solving_With_C++
Walter_Savitch
2011
123457
cd
9.99
32
Sigh_No_More
Mumford_and_Sons
2010
123458
dvd
17.99
15
Red_State
Kevin_Smith
2011
123459
cd
9.99
16
The_Church_Of_Rock_And_Roll
Foxy_Shazam
2012
123460
dvd
59.99
10
The_Walking_Dead_Season_1
Robert_Kirkman
2011
所有的都在新线上,没有空格。
基本上,它应该启动、运行read_all_records
函数并将.dat
数据放入数组中。然而,我把cout << records[count].item_id;
放在while循环中,只是为了看看数据是否真的进入,每次都得到-858993460
。之后,它应该运行下一个函数3次,并返回每本书的数量。
您在item_price
上使用了整数类型int
。invfile >> records[count].item_price
将只提取69
而不是69.99
,因此在尝试提取year_published
时会导致错误。
请改用float
或double
。
struct record
{
int item_id;
string item_type;
float item_price;
int num_stock;
string item_title;
string item_author;
int year_published;
};
/* skipped identical lines */
while(invfile.good() && count < max_array)
{
invfile >> records[count].item_id >> records[count].item_type
>> records[count].item_price >> records[count].num_stock
>> records[count].item_title >> records[count].item_author
>> records[count].year_published;
cout << records[count].item_price << endl;
if(!invfile.good())
break;
cout << records[count].item_id << endl;
count++;
}
invfile.close();
请注意,在for (int count = 0; count<max_array; count++);
中有一个额外的分号。我想你不是故意的,所以把它删除。
这不是问题的直接答案,但可能在重构后会消失:
std::istream& operator>>(std::istream& is, record& r) {
return is >> r.item_id >> r.item_type >> … >> r.year_published;
}
int main () {
if (std::ifstream invfile("inventory.dat")) {
std::vector<record> records((std::istream_iterator<record>(invfile)),
std::istream_iterator<record>());
num_inventory_of_type(records, "dvd");
num_inventory_of_type(records, "cd");
num_inventory_of_type(records, "book");
}
}
如果你仍然想在阅读时打印出每条记录,代码可以按如下方式重新排列:
std::vector<record> records;
for (std::istream_iterator<record> i(invfile);
i != std::istream_iterator<record>(); ++i)
{
records.push_back(*i);
std::cout << i->item_id << "n";
}
您需要更改int item_price;到float so->float item_price;
如上所述,您需要交换计数++;并且cout<lt;records[count].item_id行。
经过这两次更改后,它将正常工作。
struct record
{
int item_id;
string item_type;
float item_price; // <--- Needs to be a float
int num_stock;
string item_title;
string item_author;
int year_published;
};
// This order is required because you are storing in the current 'count' record and then you need to print it. Then increment the count to store the next record
cout << records[count].item_id;
count++;
相关文章:
- 我的程序没有从文件中读取并输入数据
- 输入数据成员未按要求工作
- 使用 pybind11 调用 Python 函数时避免复制输入数据
- 使用 python 或 c ++ 中的 sql 根据输入数据输出输出示例
- c++用输入数据创建数组
- Caffe 或 Caffe2 可以直接从 GPU 获得输入数据吗?
- C++ QTextDocument::contentChange() 信号提供无效的输入数据
- boost::asio 以async_read复制输入数据
- 输入数据类型检查循环未按预期工作 (C++)
- SVM 训练时间是否取决于输入数据的内容?
- 将用户输入数据相加
- 在线评委如何传递输入数据?
- 如何传递私有地图和输入数据
- C++ 将用户输入数据用于多个函数
- 通过按空格键结束键盘输入数据,而不是输入键 c/c++
- 如何为作为雇员类的指针传入的构造函数输入数据
- C++:检查输入数据
- 关于输入数据
- 防止在管道中输入数据时退出
- C/C++中固定长度实输入数据的高效二维FFT