Я пытался отладить свою программу, но это не помогает. Я использовал циклы WHILE только для помещения данных в массивы, но я подумал, что использование цикла FOR здесь будет проще.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct record
{
int item_id;
string item_type;
int item_price;
int num_stock;
string item_title;
string item_author;
int year_published;
};
void read_all_records(record records[]);
int num_inventory_of_type(record records[]);
const int max_array = 100;
int main()
{
record records[max_array];
read_all_records(records);
cout << records[1].item_author;
num_inventory_of_type(records);
return 0;
}
void read_all_records(record records[])
{
ifstream invfile;
invfile.open("C:\\Users\\acsindle\\Dropbox\\Prog2\\Asg22\\Asg22\\inventory.dat");
if (!invfile.is_open())
{
cout<<"file open failed";
exit(1);
}
int slot = 0;
for (int count = 0; count<max_array; count++)
{
invfile >> records[slot].item_id >>
records[slot].item_type >>
records[slot].item_price >>
records[slot].num_stock >>
records[slot].item_title >>
records[slot].item_author >>
records[slot].year_published;
slot++;
}
invfile.close();
}
int num_inventory_of_type(record records[])
{
int slot = 0;
int book = 0;
int dvd = 0;
int cd = 0;
for (int count = 0; count>max_array; count++);
{
if (records[slot].item_type == "book")
book++;
if (records[slot].item_type == "dvd")
dvd++;
if (records[slot].item_type == "cd")
cd++;
}
return book, dvd, cd;
}
Мой файл .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
Все они разделены на одной строке без пробелов, поэтому нет необходимости их склеивать. Это должно быть так же просто, как cin и сохранено. Не уверен, что это мой цикл FOR, который испортил или это проблема с передачей массива. Также мой массив связан с моей структурой, есть ли термин для этого? Это то, что считается многомерным массивом?