I have a program that is menu driven by functions. The program gathers inventory data from the user and then the user can add, change, display (by record #), or display all records. The code works great, BUT when I add records and go to edit them, it changes the quanitity and price for all records and not just one.

For example: I run the program and add 3 records. 0-1

record 0 - description: zero
quanity: 0
price 0
record 1 description: one
quanity: 1
price 1
record 2 description: two
quanity: 2
price 2
I then go to edit a record for example record 1
I change the description to: one(new)
quanitity: 99
price: 99
Now, if I display all records, the information is correct.
IF I go to look at one record FOR EXMAPLE: record 0 it gives results:
description: zero
quanitity: 2
price: 2

Since the record was not edited by the program why did the price and quanitity change?

Any help would be GREAT. I have been working on this code for a while now and its starting to frustrate me.

Thank you in advance.

#include <iostream>
#include <fstream>
using namespace std;

const int DESC_SIZE = 31;
const int NUM_RECORDS = 5;

struct InventoryItem
{
    char desc[DESC_SIZE];
      int qty;
    double price;
    
};
InventoryItem record = {"",0,0.0};
long recNum;
    char choice;

void displayRecord(){
    fstream inventory("Inventory.dat", ios::in | ios::binary);

    inventory.read(reinterpret_cast<char *>(&record), sizeof(&record));

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

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

    cout << "Description: ";
    cout << record.desc << endl;
    cout << "Quanitity: ";
    cout << record.qty << endl;
    cout << "Price: ";
    cout << record.price << endl;

    inventory.close();

}

void changeRecord(){
 fstream inventory("Inventory.dat", ios::in | ios::out | ios::binary);

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

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

    cout << "Description: ";
    cout << record.desc << endl;
    cout << "Quanity: ";
    cout << record.price << endl;
    cout << "Price: ";
    cout << record.price << endl;

    cout << "Enter the new data:\n";
    cout << "Description: ";
    cin.ignore();
    cin.getline(record.desc, DESC_SIZE);
    cout << "Quanitity: ";
    cin >> record.qty;
    cout << "Price: ";
    cin >> record.price;

    inventory.seekp(recNum * sizeof(record), ios::beg);

    inventory.write(reinterpret_cast<char *>(&record), sizeof(record));

    inventory.close();
}

void addRecord(){
    char again;
    fstream inventory("Inventory.dat", ios::out | ios::binary);

    //get data about a aperson
do 
{
cout << "Enter the following data about the item: ";
cout << "Description: ";
cin >> record.desc;
//cin.getline(record.desc, DESC_SIZE);
cout << "Quanitity: " ;
cin >> record.qty;
cin.ignore();
cout << "Price: ";
cin >> record.price;
cin.ignore();

//write the contents of the person structure to file
inventory.write(reinterpret_cast<char *>(&record), sizeof(record));

//determine whether the user wants to write another record
cout << "Do you want to enter another record?";
cin >> again;
cin.ignore();
}while (again == 'Y' || again == 'y');

//close the file
inventory.close();

}

void showAll(){
    fstream inventory("Inventory.dat", ios::in | ios::binary);
    inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
    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();
}

void mainMenu(){
    char choice;
    bool isDone = false;

    while (! isDone)
    { cout << "What would you like to do?" << endl;
    cout << "Please select an option below: " << endl;
    cout << "\t1. Add new record" << endl;
    cout << "\t2. Display a record" << endl;
    cout << "\t3. Change any record" << endl;
    cout << "\t4. Display all records" << endl;
    cout << "\t5. Quit Program" << endl;

    cin >> choice;

    switch (choice)
    {
    case '1':
        //cout << "hi" << endl;
        addRecord();
        break;
    case '2':
        displayRecord();
        break;
    case '3':
        changeRecord();
        break;
    case '4':
        showAll();
        break;
    case '5':
        isDone = true;
        break;
    default:
        cout << "Invalid choice!" << endl;
        cout << "Please select a valid option" << endl;
        cout << "" << endl;
}


    }}







int main ()
{

   // setupBlank();
    mainMenu();
 
    return 0;
}

Since the record was not edited by the program why did the price and quanitity change?

In the displayRecord() there is an extra inventory.read(...) at the start of the function. So the position is hence off by one.
And then a little error where the address of operator & has slipped in, a couple of lines later inventory.read(reinterpret_cast<char *>(&record), sizeof( & record)); Then addRecord() needs attention, it will truncate the file. So you need to figure out the proper mode for opening the file.

This article has been dead for over six months. Start a new discussion instead.