running an inventory program when editing existing record ex record 2 of 3 and displ 5189165
Running an inventory program. When editing existing record (ex: record 2 of 3) and display the edit, only records 1
and 2 show up. Record 3 is deleted.
Results:
MENU
1. Add Record
2. Display Record
3. Edit Record
4. Exit
Enter your selection: 1
Enter the item description: Laptop
Enter the quantity: 0
Quantity must be 1 or more.
Enter the quantity: 15
Enter the wholesale cost: 499.00
Enter the retail cost: 0
Cost must be greater than 0.
Enter the retail cost: 659.00
Enter the date as mm-dd-yyyy: 07-21-2017
Enter another record?
Enter Y for yes: y
Enter the item description: Mouse
Enter the quantity: 0
Quantity must be 1 or more.
Enter the quantity: 21
Enter the wholesale cost: 3.75
Enter the retail cost: 8.99
Enter the date as mm-dd-yyyy: 7-21-2017
Enter another record?
Enter Y for yes: y
Enter the item description: Keyboard
Enter the quantity: 17
Enter the wholesale cost: 4.50
Enter the retail cost: 12.49
Enter the date as mm-dd-yyyy: 7-21-2017
Enter another record?
Enter Y for yes: n
MENU
1. Add Record
2. Display Record
3. Edit Record
4. Exit
Enter your selection: 2
Description Laptop
Quantity 15
Wholesale cost 499
Retail cost 659
Date 07-21-2017
Description Mouse
Quantity 21
Wholesale cost 3.75
Retail cost 8.99
Date 7-21-2017
Description Keyboard
Quantity 17
Wholesale cost 4.5
Retail cost 12.49
Date 7-21-2017
MENU
1. Add Record
2. Display Record
3. Edit Record
4. Exit
Enter your selection: 3
Enter the record number to edit: 2
Description Mouse
Quantity 21
Wholesale cost 3.75
Retail cost 8.99
Date 7-21-2017
Enter the new description: Mouse
Enter the quantity: 10
Enter the wholesale cost: 3.50
Enter the retail cost: 8.99
Enter the date: 7-25-2017
MENU
1. Add Record
2. Display Record
3. Edit Record
4. Exit
Enter your selection: 2
Description Laptop
Quantity 15
Wholesale cost 499
Retail cost 659
Date 07-21-2017
Description Mouse
Quantity 10
Wholesale cost 3.5
Retail cost 8.99
Date 7-25-2017
MENU
1. Add Record
2. Display Record
3. Edit Record
4. Exit
Enter your selection:
Program:
/***************************************************
This program demonstrates an inventory allowing user
to add, edit, and display records in a file.
***************************************************/
#include
#include
using namespace std;
const int DESCRIPTION = 20;
// Function prototype
void Add();
void Display();
void Edit();
// Structure definition
struct Inventory
{
char item[DESCRIPTION];
int qty;
double wholeCost;
double retailCost;
char date[15];
};
int main()
{
// Variables
int select;
do
{
// Menu
cout
cout
cout
cout
cout
cout
cin >> select;
cout
switch (select)
{
case 1:
Add();
break;
case 2:
Display();
break;
case 3:
Edit();
break;
case 4:
break;
default:
cout
break;
}
} while (select != 4);
system(“pause”);
return 0;
}
// Function Add
void Add()
{
fstream fout;
const int size = 3;
char ch;
fout.open(“Records.txt”, ios::out);
Inventory inv;
do
{
cout
cin.ignore();
cin.getline(inv.item, DESCRIPTION);
cout
cin >> inv.qty;
while (inv.qty
{
cout
cout
cin >> inv.qty;
}
cout
cin >> inv.wholeCost;
while (inv.wholeCost
{
cout
cout
cin >> inv.wholeCost;
}
cout
cin >> inv.retailCost;
while (inv.retailCost
{
cout
cout
cin >> inv.retailCost;
}
cout
cin.ignore();
cin.getline(inv.date, 15);
fout.write(reinterpret_cast(&inv), sizeof(inv));
cout
cout
cin >> ch;
cout
} while (ch == 'Y' || ch == 'y');
fout.close();
}
// Function Display
void Display()
{
fstream fout;
fout.open(“Records.txt”, ios::in);
Inventory inv;
fout.read(reinterpret_cast(&inv), sizeof(inv));
while (!fout.eof())
{
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
fout.read(reinterpret_cast(&inv), sizeof(inv));
}
fout.close();
cout
}
// Function Edit
void Edit()
{
fstream fout;
fout.open(“Records.txt”, ios::in | ios::out);
Inventory inv;
int recordNum;
cout
cin >> recordNum;
recordNum -= 1;
fout.seekg(recordNum * sizeof(inv), ios::beg);
fout.read(reinterpret_cast(&inv), sizeof(inv));
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cin.ignore();
cin.getline(inv.item, DESCRIPTION);
cout
cin >> inv.qty;
while (inv.qty
{
cout
cout
cin >> inv.qty;
}
cout
cin >> inv.wholeCost;
while (inv.wholeCost
{
cout
cout
cin >> inv.wholeCost;
}
cout
cin >> inv.retailCost;
while (inv.retailCost
{
cout
cout
cin >> inv.retailCost;
}
cout
cin.ignore();
cin.getline(inv.date, 15);
cout
// Move back to the beginning of this record's position
fout.seekg(recordNum * sizeof(inv), ios::beg);
// Write new record over the current record
fout.write(reinterpret_cast(&inv), sizeof(inv));
fout.close();
}