File input / output binary

Hey guys, I'm having some really hard trouble figuring this out. I'm making a program that allows me to store a structure of records in a file, I pretty much have it done except I cannot get this one part! I'm trying to make it to allow the user to change the record if they want to, for some reason I can't get it to work! Any insight would be appreciated thank you!

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE1 = 51;

struct Inventory
{
char itemD[SIZE1];
int qty;
double wholeS;
double retailS;
char date[SIZE1];
};

int main()
{
Inventory inven;
fstream fObj;
char choice;
char again;



do
{

cout << "Choose from the following options: \n\n";
cout << "1. Add a new record\n";
cout << "2. Display a record\n";
cout << "3. Change a record\n";
cout << "4. Quit\n\n";
cout << "Choice: ";
cin.get(choice);
cin.ignore();
cout << endl;

switch(choice)
{
case '1':
do
{
fObj.open("inventory.txt", ios::out | ios::binary | ios::app);
cout << "Enter Item Description: ";
cin.getline(inven.itemD, SIZE1);
cout << "Enter Quantity on Hand: ";
cin >> inven.qty;
cout << "Enter Wholesale Price: ";
cin >> inven.wholeS;
cout << "Enter Retail Price: ";
cin >> inven.retailS;
cin.ignore();

cout << "Enter Date Added: ";
cin.getline(inven.date, SIZE1);
cout << endl;
fObj.write(reinterpret_cast<char *>(&inven), sizeof(inven));
fObj.close();
cout << "Another 1? : ";
cin.get(again);
cin.ignore();
}
while(again == 'y' || again == 'Y');

break;


case '2':

fObj.open("inventory.txt", ios::in | ios::binary);
fObj.read(reinterpret_cast<char *>(&inven), sizeof(inven));

while(!fObj.eof())
{
cout << "Item Description: " << inven.itemD << endl;
cout << "Quantity on Hand: " << inven.qty << endl;
cout << "Wholesale Price: " << inven.wholeS << endl;
cout << "Retail Price: " << inven.retailS << endl;
cout << "Date Added: " << inven.date << endl;
cout << "\nPress Enter for next Record: ";
cin.get();

fObj.read(reinterpret_cast<char *>(&inven), sizeof(inven));
if (fObj.eof())
cout << "\nEnd of File Reached!\n\n";
}

fObj.close();
break;

case '3':
fObj.open("inventory.txt", ios::in | ios::binary);
fObj.read(reinterpret_cast<char *>(&inven), sizeof(inven));
char ans;


cout << "Item Description: " << inven.itemD << endl;
cout << "Quantity on Hand: " << inven.qty << endl;
cout << "Wholesale Price: " << inven.wholeS << endl;
cout << "Retail Price: " << inven.retailS << endl;
cout << "Date Added: " << inven.date << endl;
fObj.close();
cout << "Would you like to change this record? Y/N: ";
cin.get(ans);
cin.ignore();

if(ans == 'y' || ans == 'Y')
{
fObj.open("inventory.txt", ios::out | ios::binary);
fObj.write(reinterpret_cast<char *>(&inven), sizeof(inven));
cout << "New Item Description: ";
cin.getline(inven.itemD, SIZE1);
cout << "New Quantity: ";
cin >> inven.qty;
cin.ignore();
cout << "New Wholesale Price: ";
cin >> inven.wholeS;
cin.ignore();
cout << "New Retail Price: ";
cin >> inven.retailS;
fObj.close();
cin.ignore();
}
if(ans == 'n' || ans == 'N')
{
break;
}


fObj.close();

break;
case '4':
exit(0);

}

}
while(choice != 4);
}
Please use code tags when posting code. Highlight the code and click the <> button to the right of the edit window.

One problem is that you're writing the object before you prompt for the changes.

The other problem is that you only check the first object in the file. What if they want to edit the 7th?
Yeah, I figured out the writing before prompting for the changes, but I'm still having trouble. What do you mean? Sorry, I am confused =/
It looks like the file can store more than one object but the code to edit an object will only let you edit the first one in the file. That's what I mean.
Topic archived. No new replies allowed.