Suzie Scifi and Joe Sixpak have donated their time to the Food pantry for their
ID: 3697594 • Letter: S
Question
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.
Soup
500
Peas
200
Beans
1000
Ravioli
300
Olives
333
Spam
665
Hash
200
Explanation / Answer
Try the below c++ code, I think it may help you to an extent.
Please check your requirements after copying the below code, becoz its only the skeleton code..
PLEASE DO RATE if the below code helps to your requirement....
#include<iostream>
#include<iomanip>
#include<string>
#include<array>
#include<fstream>
using namespace std;
int getCannedNumber(string cname[], int cnumber[], string search)
{
int num= 0;
for(int i = 0; i < cname.size(); i++)
{
if (search == cname[i])
flag = cnumber[i];
}
return num;
}
void storeCannedDetails(string cname[], int cnumber[])
{
ofstream fout;
fout.open("pantry.txt");
if(fout.is_open())
{
for(int i = 0; cname.size(); i++)
{
fout << cname[i] << " - " << cnumber[i] << endl; //writing ith element of array in the file
}
cout << "The file pantry.txt has been created..." << endl;
}
else //file could not be opened
{
cout << "Problem in creating the file..." << endl;
}
}
int main()
{
string can_name[7] = {"Soup","Peas","Beans","Ravioli","Olives","Spam","Hash"};
int can_num[7] = {500,200,1000,300,333,665,200};
string gname;
int gnum = 0;
int flag = 0;
cout<<"Enter the Canned good Name ";
cin>>gname;
if(gname == "")
cout << "Invalid good name ";
else if(gname == "ZZZ" || gname == "zzz")
{
storeCannedDetails(can_name,can_num);
cout << "Program Exited ";
}
else
{
gnum = getCannedNumber(can_name,can_num,gname);
if(gnum==0)
cout<<"Canned good is not in the data-base" << endl;
else
cout<<"Good Name: " << gname << " and Number: " << gnum << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.