Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- const int DES = 20;
- //declare variables
- void Add();
- void Display();
- void Edit();
- void Exit();
- struct Inventory
- {
- char item[DES];
- int qty;
- int wcost;
- int rcost;
- char date[10];
- };
- //Main function
- int main()
- {
- int choice;
- do
- {
- cout << "1. Add a new record: " << endl;
- cout << "2. Display a previous record: " << endl;
- cout << "3. Edit an old record:" << endl;
- cout << "4. Exit the program:" << endl;
- cin >> choice;
- switch (choice)
- {
- case 1:
- Add();
- break;
- case 2:
- Display();
- break;
- case 3:
- Edit();
- break;
- case 4:
- Exit();
- break;
- default: cout << "Invalid Selection" << endl;
- }
- } while
- (choice <= 2);
- system("PAUSE");
- return 0;
- }
- //Add funtion
- void Add()
- {
- fstream fout;
- const int size = 3;
- char ch;
- int i = 0;
- fout.open("Records.txt", ios::out | ios::ate);
- Inventory inv;
- //get data
- do
- {
- cout << "Enter item description: " << endl;
- cin.ignore();
- cin.getline(inv.item, DES);
- cout << "Enter quantity: " << endl;
- cin >> inv.qty;
- cout << "Enter wholesale cost: " << endl;
- cin >> inv.wcost;
- cout << "Enter retail cost: " << endl;
- cin >> inv.rcost;
- cout << "Enter date: " << endl;
- cin.ignore();
- cin.getline(inv.date, 10);
- //write record to file
- fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
- cout << "Do you want to add another record? " << endl;
- cin >> ch;
- } while
- (ch == 'Y' && 1 < 3);
- //close the file
- fout.close();
- }
- //"Display" function
- void Display()
- {
- fstream fout;
- fout.open("Records.txt", ios::in);
- Inventory inv;
- fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
- while (!fout.eof())
- {
- cout << "\nDescription\t: ";
- cout << inv.item;
- cout << "\nQuantity\t: ";
- cout << inv.qty;
- cout << "\nWholesale Cost\t: ";
- cout << inv.wcost;
- cout << "\nRetail Cost\t: ";
- cout << inv.rcost;
- cout << "\nDate\t: ";
- cout << inv.date;
- fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
- }
- //close the file
- fout.close();
- void edit
- {
- fstream fout;
- fout.open("Records.txt", ios::in);
- Inventory inv;
- fout.open("Records.txt", ios::out | ios::ate);
- Inventory inv;
- //get data
- do
- {
- cout << "Enter item description: " << endl;
- cin.ignore();
- cin.getline(inv.item, DES);
- cout << "Enter quantity: " << endl;
- cin >> inv.qty;
- cout << "Enter wholesale cost: " << endl;
- cin >> inv.wcost;
- cout << "Enter retail cost: " << endl;
- cin >> inv.rcost;
- cout << "Enter date: " << endl;
- cin.ignore();
- cin.getline(inv.date, 10);
- //write record to file
- fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
- cout << "Do you want to add another record? " << endl;
- cin >> ch;
- } while
- (ch == 'Y' && 1 < 3);
- //close the file
- fout.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement