Visual Basic Suzie Scifi and Joe Sixpak have donated their time to the Food pant
ID: 3697798 • Letter: V
Question
Visual Basic
Suzie Scifi and Joe Sixpak have donated their time to the Food pantry for their ACA 115 course. They spent many hours during the spring semester stocking shelves with can goods so that that Pantry could operate during the summer. Below is a table that shows the can goods by name and the number in inventory. Soup 500 Peas 200 Beans 1000 Ravioli 300 Olives 333 Spam 665 Hash 200 Suzie and Joe want you to write a program that uses parallel arrays one for the canned good and one for the number of units. The program will prompt the user to type in the canned good name and the program outputs the name and the number in inventory. If the canned good name is not in the array, the program should display a message that the canned good is not in the “data-base” and prompt for another input. Also the program should continue to prompt for canned good names until the user enters “ZZZ” to exit the program. Before exiting the program Joe and Suzie want the program to create a file that contains the canned good name and number in inventory and displays a message that the file has been created. Name this file pantry.txt. Suzie and Joe require you to write a function that accepts both arrays and the canned good name entered by the user and returns the number in inventory to the main subroutine which handles all input and output. They also want a separate subroutine that writes the name of the canned good and the number of units in the arrays to a file as indicated below. The program will also display a message that the file has been created in the main subroutine.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
const int DESC_SIZE = 51; // holds inventory description size
const int DATE_SIZE = 11; // holds date size
// Declaration of InventoryItem structure
struct InventoryItem
{
char desc[DESC_SIZE]; // description holds 31 charaters
int quantity; // variable to hold quantity
char date[DATE_SIZE]; // variable to hold date
int menu();
};
void addRec(fstream &); // function prototype to add a record
void dispRec(fstream &); // function prototype to view a record
void chgRec(fstream &); // function prototype to change a record
int main()
{
long selection; // variable to hold menu selection
int menu();
fstream inventory ("Inventory.dat", ios::in | ios::out | ios::binary);
InventoryItem record = {" ", 0, 0.0};
cout << fixed << showpoint << setprecision(2);
cout<<"Inventory Managment"<<endl;
// write the blank records
for (int count = 0; count < 5; count++)
{
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
}
inventory.close();
inventory.open("Inventory.dat", ios::out | ios::binary);
while (selection != 4)
{
switch(selection)
{
case 1: // Add a new record
{
addRec(inventory);
break;
}
case 2: //View record
{
dispRec(inventory);
break;
}
case 3: //Change record
{
chgRec(inventory);
break;
}
default: //Invalid selection
{
cout << "Invalid selection" << endl;
}
selection = menu();
}
}
inventory.close();
system("pause");
return 0;
}
int menu()
{
long selection;
int choice;
selection = menu();
cout << "Please make a selection, 1 through 4." << endl;
cout << "1. Add a new record" << endl;
cout << "2. View an exisitng record" << endl;
cout << "3. Change an exisitng record" << endl;
cout << "4. Exit" << endl;
cout << endl;
cout <<"Enter your choice (1-4): ";
cin >> choice;
while(choice < 1 || choice > 4)
{
cout << "Invaild selection!" << endl;
cout << "Please enter your choice (1-4) : "<< endl;
cin >> choice;
}
system("pause");
return choice;
}
void addRec(fstream &file) // add new information to inventory
{
cout << "Enter the following inventory data:"<<endl;
fstream inventory("Inventory.dat", ios::out | ios::binary);
InventoryItem record;
//Get new data
cout << "Description: ";
cin.ignore();
cin.getline(record.desc, 51);
cin >> record.desc;
cout << "Quantity: ";
cin >> record.quantity;
cout << "Date added to inventory (in 00/00/0000 format): ";
cin >> record.date;
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Record added to file."<<endl;
file.close();
}
// view record
void dispRec(fstream &file)
{
fstream inventory ("Inventory.dat", ios::out | ios::binary);
InventoryItem record;
long recNum;
cout << "Enter the record number of the item to view:";
cin >> recNum;
// Go to desired record and read it.
inventory.seekg(recNum * sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.quantity << endl;
cout << "Date (in 00/00/0000 format): " << record.date << endl;
if(file.fail())
file.clear();
file.close();
}
void chgRec(fstream &file) // Change a record
{
fstream inventory ("InventoryFile.dat", ios::out | ios::binary);
InventoryItem record;
long recNum;
cout << "Enter the record number of the item you want to edit: ";
cin >> recNum; //Go to record and read it.
inventory.seekg(recNum * sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.quantity << endl;
cout << "Date (in 00/00/0000 format): " << record.date << endl;
inventory.seekp(recNum * sizeof(record), ios::beg);
// Overwrite current record
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
//Close
inventory.close();
}
Edit & Run
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.