really need help,,,,, please....+++++++++

Combine the three parts (above) and add a menu to:
1) initialize,
2) output all records,
3) edit a record,
4) exit program.

i can not do initialize . can somebody explain to me ? thank you.


this is my code.




#include <iostream>
#include <fstream>
using namespace std;
const int DESC_SIZE = 31,NUM_RECORDS = 5;
struct inventory
{
char desc[DESC_SIZE];
int qty;
double price;


};

int menu();
int initialize(inventory record);
void displayAll(inventory record, fstream &, int NUM_RECORDS);
void modify(inventory record, fstream &, int NUM_RECORDS);


int main()
{
int choice;
int recordCounter=0;

inventory record ;


fstream inventory("invtry.dat", ios::in | ios::out | ios::binary);

if(!inventory.eof())
{
cout<<"was not initialize";
cout<<"\n";
}


do
{
choice = menu();

switch(choice)
{
case 0: initialize(record);
break;

case 1:displayAll (record, inventory, NUM_RECORDS);
break;

case 2: modify (record, inventory, NUM_RECORDS);
break;
case 3: cout<<"Exiting Program Good Bye"<<"\n";
break;
}
}
while(choice!=3 || choice <0 || choice >3);


recordCounter = initialize(record);

inventory.close();
return 0;
}

int initialize(inventory record)
{
int recordCounter=0;


fstream inventory("invtry.dat", ios::in | ios::out | ios::app| ios::binary);

if(!inventory.eof())
{
cout<<"There was an error reading the data file"<<"\n";
}

else
{
int NUM_RECORDS = 5;
for (int count = 0; count < NUM_RECORDS; count++)
{
cout << "Now writing record " << count << endl;
inventory.write(reinterpret_cast<char *>(&record), sizeof(record));

}

inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
inventory.seekg(0L, ios::beg);
while (!inventory.eof())
{
cout << "Description: ";
cout << record.desc << endl;
cout << "Quantity: ";
cout << record.qty << endl;
cout << "Price: ";
cout << record.price << endl << endl;
inventory.read(reinterpret_cast<char *>(&record), sizeof(record));

}


}
inventory.close();
return 0;
}


int menu()
{
int choice;

cout<<"Please make a menu choice for the task you which to accomplish"<<"\n";
cout<<"\n";
cout<<" 0: initialize records to the file"<<"\n";

cout<<" 1: Display all records in the file"<<"\n";
cout<<" 2: Change any record in the file"<<"\n";
cout<<" 3: Quit this program"<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"Enter menu choice now ";
cin>>choice;
cout<<"\n";

if (choice <0 || choice > 3)
cout<<"You must select a numeric choice from the menu (0, 1, 2, 3)"<<"\n";

return choice;
}



inventory.write(reinterpret_cast<char*>(&record),sizeof(record));
*NUM_RECORDS = *NUM_RECORDS+1;
return 0;
}



void displayAll(inventory record, fstream &inventory, int NUM_RECORDS)
{


for(int i=0; i<NUM_RECORDS; i++)
{

inventory.seekg(i *sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char*>(&record), sizeof(record) );
cout<<"Record Number: "<<i<<"\n";
cout<<"Description: "<<record.desc<<"\n";
cout<<"qty : "<<record.qty<<"\n";
cout<<"Price: "<<record.price<<"\n";


}
}

void modify(inventory record, fstream &inventory, int NUM_RECORDS)
{
int recNum;

cout<<"Which record do you want to edit? ";
cin>>recNum;

if ( (recNum > (NUM_RECORDS-1) ) || (recNum < 0 ))
{
cout<<"That record does not exist and can't be edited! "<<"\n";
}
else
{
inventory.seekg(recNum *sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char*>(&record), sizeof(record));

cout<<"The current item description: "<<record.desc<<"\n";
cout<<"The current qty: "<<record.qty<<"\n";
cout<<"The current Price: "<<record.price<<"\n";

cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"The new description for item: ";
cin.ignore();
cin.getline( record.desc,31);
cout<<"\n";
cout<<"The new qty is: ";
cin>>record.qty;
cout<<"\n";
cout<<"The new Price is: ";
cin>>record.price;
cout<<"\n";



inventory.seekp(recNum *sizeof(record), ios::beg);
inventory.write(reinterpret_cast<char*>(&record),sizeof(record));

}


}
Last edited on
anyone knows?
What do you mean by "i can not do initialize" ?
i mean when i run first menu 0.initialize is wrong.
initialize is wrong

What does that mean ?
I need a description of whats wrong.
Topic archived. No new replies allowed.