Mailinglist Archive: opensuse-programming (68 mails)

< Previous Next >
Re: [suse-programming-e] C++ compile problem. error in function
  • From: Ged <ged.suse@xxxxxxxxxxxx>
  • Date: Tue, 13 Jan 2004 20:33:38 +0000
  • Message-id: <200401132033.38141.ged.suse@xxxxxxxxxxxx>

It seems to stick on 11. It won't always let me input item number 12.
The items i'm trying to enter are as follows.

Code Description price
------ -------------- ------
1000 Salt 0.56
1001 Rock Salt 0.89
1010 Peppercorns 1.59
1020 Plain Flour 1.11
1021 Wholemeal Flour 1.35
1022 Self Raising Flour 1.40
1030 Cane Sugar 1.20
1031 Icing Sugar 1.30
1032 Brown Sugar 1.35
1040 White Rice 1.10
1041 Brown Rice 1.15
1050 Yeast 4.50
1051 Dried Yeast 3.65

If I follow it in that order, I am not able to get past item number 11.
If I enter random data however, (just tapping any old input) I don't have any
problems.
I've been chasing this bug for ages now...heh.

If anyone gets a spare 20 mins, I would be greatfull if you could enter this
data and see if you find the same bug. I have only tried this on a Windows
machine, but I think if <conio.h> is swapped for <curses.h> (for getch()) and
if <stdlib.h> is added for the system call (obviously the function needs
swapping from 'cls' to 'clear') then everything should work OK.

I have attached the file just in case there is a good samaritan :)
(i'm also stuck on formating the print_bill() output)

Thanks,

Ged


On Tuesday 13 January 2004 15:57, Trey Gruel wrote:
> > Thanks Trey / Charly / Jerry.
>
> no prob
>
> > Anyway, I have a very strange bug in my program which I can't seem to
> > iron out. When I add more stock via the number1 in the menu, everything
> > is fine and I can build the stock up, however, if I then try and save
> > this to file (via menu num 4) is doesn't always save my new stock, it
> > stays as it was.
> > e.g. I currently have 11 items in stock. If I go to add another item, it
> > doesn't seem to save them.....I thought maybe it wasn't updating
> > 'num_stock_items' but am unsure.
>
> i had to make a couple small changes to get it to compile on the redhat
> 7.3 box i'm sitting at right now (get rid of conio.h/getch and no call to
> system(cls)), but i was unable to reproduce the problem.
>
> i tried a couple runs of:
>
> 1) enter 2-3 items
> 2) display them
> 3) save to disk
> 4) quitting
> 5) restart the prog
> 6) load from disk
> 7) display them
> 8) goto 1
>
> every time, all records were saved and loaded.
>
> one comment on your file io: it's a good idea to close the file after
> you're done with it.
>
> --
> trey

--
-=SuSE Linux 9.0=-



#include <iostream>
#include <fstream>
#include <conio.h> //for getch()
#include <iomanip>

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;
float weight;
float cost;
}bill_line[50];
/*
struct bill
{
bill_line items[20];
int num_lines;
};
*/
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;
cls();

return (bill);
}


void print_bill ( BillLine bill_line[50], int num_bill_items )
{
cls();
cout << "THE CORNER SHOP\n\n";
cout << "Product " << setw(20) << "Kilos" << setw(20) << "Cost(£)" << endl;

for (int i=0; i<num_bill_items; ++i)
{
cout << bill_line[i].stock.desc << setw(20) << bill_line[i].weight
<< setw(20) << bill_line[i].cost << endl;
}
cls();
}


< Previous Next >
References