Question
Write two programs which will work with a structure. a) The first program will use a structure to store the following inventory data in a
Write two programs which will work with a structure.
a) The first program will use a structure to store the following inventory data in a file:
Item Description
Quantity on Hand
Wholesale Cost
Retail Cost
Date Added to Inventory
The program will write five records to an output file for different items; the quantity, costs, and date for each item will be different from the other items.
b) The second program will read the file created by the first program using the same structure. The second program will calculate and display the following information:
total wholesale value of the inventory
total retail value of the inventory
total quantity of all items in the inventory
#include "stdafx.h" #include
const int DES = 20;
//declare variables void Add(); void Display(); void Edit();
struct Inventory { char item[DES]; int qty; int wcost; int rcost; char date[10]; };
//Main function int main() { int choice; do { cout << "MENU" << endl; cout << "1. Add Record: " << endl; cout << "2. Display Records: " << endl; cout << "Please enter your selection." << endl; cin >> choice;
switch (choice) { case 1: Add(); break; //Add record case 2: Display(); break; //Display record
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::cout | ios:: app); 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
//close the file fout.close(); }
//"Display" function void Display() { fstream fout; fout.open("Records.txt", ios::in); Inventory inv; fout.read(reinterpret_cast
cout << " Description\t: "; cout << inv.item; cout << " Quantity\t: "; cout << inv.qty; cout << " Wholesale Cost\t: "; cout << inv.wcost; cout << " Retail Cost\t: "; cout << inv.rcost; cout << " Date\t: "; cout << inv.date; fout.read(reinterpret_cast
ERRORS:
class "std::basic_ios
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started