Can anyone please, please show me what's going wrong here. (Full code
listed at end of post)
I'm trying to access a structure from within another structure but am
receiving errors. Unfortunately, I need to build this on a windows
machine, but I think I have made the code fully portable for Linux. It
compiles with the mingw compiler anyway.
Structures are global and are as follows:
struct StockItem
{
int code;
char desc[20];
float price;
}stock[NUM_OF_ITEMS];
struct BillLine
{
StockItem stock;
float weight;
float cost;
}bill_line[50];
The 'stock' array is already populated.
All I'm trying to do is fill the 'BillLine bill_line' array in the first
function, and then print out the array values in the second. My error
seems to be coming from accessing one structure from another. My problem
code is as follows, I've commented the line numbers flagged up by the
compiler:
BillLine bill_item(void)
{
BillLine bill;
int item;
cls();
cout << "Enter item number purchased: ";
cin >> item;
item -= 1;
/*line 213*/ bill.stock = stock[item];
cout << "Weight: ";
cin >> bill.weight;
bill.cost = stock[item].price * bill.weight;
return (bill);
}
void print_bill ( BillLine bill_line[50], int num_bill_items ) {
for (int i=0; iBillLine::stock', which is of non-aggregate type `
StockItem[125]'
Execution terminated
The rest of the program runs fine. Although there is not much error
checking (I'll add that later) there are no warnings when compiled.
My full program code is as follows
#include <iostream>
#include <fstream>
#include //for getch()
using namespace std;
const int NUM_OF_ITEMS = 125;
struct StockItem
{
int code;
char desc[20];
float price;
}stock[NUM_OF_ITEMS];
struct BillLine
{
StockItem stock[NUM_OF_ITEMS];
float weight;
float cost;
}bill_line[50];
void cls(void) { system("cls"); } //function designed to aid
portability.
void disp_menu( void );
StockItem enter_data( void );
void delete_data( StockItem* );
void see_stock( StockItem*, int );
void writeToDisk( StockItem*, int );
int readFromDisk( StockItem* );
BillLine bill_item( void );
void print_bill ( BillLine*, int );
int main()
{
int ans, ctr;
int num_stock_items = 0;
int num_bill_items = 0;
do
{
do
{
disp_menu();
cin >> ans;
} while ((ans<1) || (ans>9));
switch (ans)
{
case 1:
stock[num_stock_items] = enter_data();
num_stock_items++;
break;
case 2:
delete_data( stock );
num_stock_items -= 1;
break;
case 3:
see_stock(stock, num_stock_items);
break;
case 4:
writeToDisk( stock, num_stock_items );
// NOT writing the whole array to disk.
break;
case 5:
num_stock_items = readFromDisk( stock );
// setting the number items we read from file
break;
case 6:
bill_line[num_bill_items] = bill_item();
num_bill_items++;
break;
case 7:
print_bill( bill_line, num_bill_items );
break;
default:
break;
}
}while (ans!=8);
return 0;
}
void disp_menu()
{
cout << "\n*** The Corner Shop ***\n\n";
cout << "Select an option: \n\n";
cout << "\t1. Enter new stock item " << endl;
cout << "\t2. Delete stock item " << endl;
cout << "\t3. See current stock " << endl;
cout << "\t4. Save stock to disk " << endl;
cout << "\t5. Load stock from disk " << endl;
cout << "\t6. Enter purchase order " << endl;
cout << "\t7. Print Bill " << endl;
cout << "\t8. Exit \n" << endl;
cout << "option> ";
}
StockItem enter_data(void)
{
StockItem stock_item;
cls();
cout << "\n\nWhat is the product code: ";
cin >> stock_item.code;
cout << "What is the product description: ";
cin.ignore();
cin.getline(stock_item.desc, 20);
cout << "What is the product price: ";
cin >> stock_item.price;
cls();
return (stock_item);
}
void delete_data(StockItem stock[NUM_OF_ITEMS] )
{
StockItem stock_temp[NUM_OF_ITEMS];
int item, n=0;
cls();
cout << "Enter item to delete ";
cin >> item;
item -= 1;
for(int i = 0; i < NUM_OF_ITEMS; i++)
{
if(i == item)
++n;
stock_temp[i] = stock[n];
++n;
}
for(int i = 0; i < NUM_OF_ITEMS; i++)
stock[i] = stock_temp[i];
cls();
}
void see_stock(StockItem stock[NUM_OF_ITEMS], int num_stock_items) {
int ctr;
cls();
cout << "\n\nHere is the stock listing:\n\n";
for (ctr = 0; ctr < num_stock_items; ++ctr)
{
cout << "Item " << ctr+1 << endl;
cout << "Product Code: " << stock[ctr].code << endl;
cout << "Product Description: " << stock[ctr].desc << endl;
cout << "Price: " << stock[ctr].price << endl;
cout << "------------------------------------------------\n";
}
cout << "\nHit any key to continue.....\n";
getch();
cls();
}
void writeToDisk( StockItem* array, int numOfItems )
{
ofstream outfile( "items.txt" );
if (outfile.bad())
{
cout << "\n*** Error opening file ***\n";
}
for( int index = 0; index < numOfItems; index++ )
outfile.write( (char*)&array[index], sizeof(StockItem) );
cls();
}
int readFromDisk( StockItem* array )
{
int numOfItems = 0;
ifstream infile( "items.txt" );
if (infile.bad())
{
cout << "\n*** Error opening file ***\n";
}
StockItem tmp_stockItem;
// read from the file until we reach EOF
while( infile.read( (char*)&tmp_stockItem, sizeof(tmp_stockItem) ) )
{
// after a successfull read we store the the item into the array
memcpy( &array[numOfItems],
&tmp_stockItem,
sizeof( array[numOfItems] ) );
numOfItems++;
}
cls();
// returning the number of array items we read from the file
return numOfItems;
}
BillLine bill_item(void)
{
BillLine bill;
int item;
cls();
cout << "Enter item number purchased: ";
cin >> item;
item -= 1;
bill.stock = stock[item];
cout << "Weight: ";
cin >> bill.weight;
bill.cost = stock[item].price * bill.weight;
return (bill);
}
void print_bill ( BillLine bill_line[50], int num_bill_items ) {
for (int i=0; i