Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <fstream>
- using namespace std;
- struct inventorydata
- {
- char description[15];
- int quantityonhand;
- float wholesalecost;
- float retailcost;
- char dateadded[15];
- };
- int main()
- {
- inventorydata record;
- char menu='Y';
- int number, recordnum=0, whichrecord, count=0;
- fstream inventory("inventorydata.dat", ios::in | ios::out | ios::binary | ios::beg);
- if (!inventory.good()) return -10000;
- while(menu == 'y'||menu == 'Y')
- {
- cout << "chose an option by typing the correct number: " << endl;
- cout << "add record: 1" << endl;
- cout << "display a record: 2" << endl;
- cout << "change record: 3" << endl;
- cin >> number;
- cout << endl;
- if (number == 1)
- {
- count+=1;
- inventorydata record = {"",0,0.0,0.0,""};
- cout << "Enter data\n";
- cout << "Description: \n";
- cin.ignore();
- cin.getline(record.description, 15);
- cout << "Quantity on hand: \n";
- cin >> record.quantityonhand;
- while (record.quantityonhand < 0){
- cout <<"Error cannot be negative\n";
- cin >> record.quantityonhand;}
- cout << "Wholesale cost: \n";
- cin >> record.wholesalecost;
- while (record.wholesalecost < 0){
- cout <<"Error cannot be negative\n";
- cin >> record.wholesalecost;}
- cout << "Retail cost: \n";
- cin >> record.retailcost;
- while (record.retailcost < 0){
- cout <<"Error cannot be negative\n";
- cin >> record.retailcost;}
- cout << "Date added: \n";
- cin >> record.dateadded;
- while (record.dateadded < 0){
- cout <<"Error a date cannot be negative.. \n";
- cin >> record.dateadded;}
- inventory.seekp(recordnum * sizeof(record), ios::beg);
- inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
- recordnum += 1;
- cout << "do something else? y for yes.\n";
- cin >> menu;
- }
- else if(number == 2)
- {
- cout << "which record would you like to display?\n";
- cin >> whichrecord;
- while (whichrecord > count)
- {
- cout << "error: record not added\n";
- cin >> whichrecord;
- }
- whichrecord -=1;
- /*fstream inventory("inventorydata.dat", ios::in | ios::out | ios::binary);
- if (!inventory.good()) return -10000;*/
- inventory.seekg(whichrecord * sizeof(record), ios::beg);
- inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
- cout << "Description: ";
- cout << record.description << "\n";
- cout << "Quantity on hand: ";
- cout << record.quantityonhand << "\n";
- cout << "Wholesale cost: ";
- cout << record.wholesalecost << "\n";
- cout << "Retail cost: ";
- cout << record.retailcost << "\n";
- cout << "Date added: ";
- cout << record.dateadded << "\n";
- //inventory.close();
- cout << "do something else? y for yes.\n";
- cin >> menu;
- }
- else if(number == 3)
- {
- cout << "which record would you like to edit?\n";
- cin >> whichrecord;
- while (whichrecord > count)
- {
- cout << "error: record not added\n";
- cin >> whichrecord;
- }
- whichrecord -=1;
- /*fstream inventory("inventorydata.dat", ios::in | ios::out | ios::binary);
- if (!inventory.good()) return -10000;*/
- inventory.seekg(whichrecord * sizeof(record), ios::beg);
- inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
- cout << "Enter data\n";
- cout << "Description: \n";
- cin.ignore();
- cin.getline(record.description, 15);
- cout << "Quantity on hand: \n";
- cin >> record.quantityonhand;
- while (record.quantityonhand < 0){
- cout <<"Error cannot be negative\n";
- cin >> record.quantityonhand;}
- cout << "Wholesale cost: \n";
- cin >> record.wholesalecost;
- while (record.wholesalecost < 0){
- cout <<"Error cannot be negative\n";
- cin >> record.wholesalecost;}
- cout << "Retail cost: \n";
- cin >> record.retailcost;
- while (record.retailcost < 0){
- cout <<"Error cannot be negative\n";
- cin >> record.retailcost;}
- cout << "Date added: \n";
- cin >> record.dateadded;
- while (record.dateadded < 0){
- cout <<"Error a date cannot be negative.. \n";
- cin >> record.dateadded;}
- inventory.seekp(whichrecord * sizeof(record), ios::beg);
- inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
- cout << "do something else? y for yes.\n";
- cin >> menu;
- }
- }
- inventory.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment