I have posted this question previously but the answer doesn\'t compile. If someo
ID: 3773421 • Letter: I
Question
I have posted this question previously but the answer doesn't compile. If someone could help I would greatly appreciate it. If you are the person who previously nswered and you make it compile. I will giive a thumbs up to this program and the one that did not compile. Thank you.
Write in C++.
The Bite-a Vite-a vitamin store needs an "expert" computer programmer to help keep track of their inventory. The store maintains all their vitamin stock information on disk. The data is organized as follows:
The first column contains the vitamin name (A, B, C, etc.)
The second column contains the unit price of a single jar of that particular vitamin.
The third column has the number of jars of that vitamin in the store.
For example:
Write a C++ program that will do the following:
1. Read data from the keyboard in the form
vitamin price quantity
2. Write and call a function that will return the total value of the store's inventory for that one vitamin product (unit_price * number_of_jars).
You will also calculate:
The average price of a vitamin
The total inventory (total number of jars)
Print the output so that it is organized as follows:
Vitamin Price Inventory Total Inventory
5. Write and call a function that will return the per unit price of a vitamin. The parameter to the function will include the vitamin name. (Hint use a vector or other container class).
--------------------------------------------------
The average price for a vitamin is: $9.98
The total store inventory is 104 jars of vitamins
A 12.95 23 K 9.99 56 Z 6.99 25Explanation / Answer
Complete Program:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
double getTotalInventory(double unit_price, int number_of_jars);
int main()
{
ifstream infile;
ofstream outfile;
infile.open("data.txt");
if(!infile)
{
cerr << "Input file cannot be opened.";
exit(EXIT_FAILURE);
}
outfile.open("output.txt");
char vitamin;
double price;
int quantity;
double total_price = 0;
int total_quantity = 0;
double average_price = 0;
outfile << left << setw(12) << "Vitamin" << right << setw(12) << "Price" << setw(12) << "Inventory" << setw(20) << "Total Inventory" << endl;
outfile << "---------------------------------------------------------" << endl;
outfile << setprecision(2) << fixed;
infile >> vitamin;
while (infile)
{
infile >> price >> quantity;
double totalInventory = getTotalInventory(price, quantity);
outfile << left << setw(12) << vitamin << right << setw(12) << price << setw(12) << quantity << setw(20) << totalInventory << endl;
total_price += totalInventory;
total_quantity += quantity;
infile >> vitamin;
}
if(total_quantity > 0)
average_price = total_price / total_quantity;
outfile << " The average price for a vitamin is: $" << average_price << endl;
outfile << "The total store inventory is " << total_quantity << " jars of vitamins" << endl;
infile.close();
outfile.close();
system("pause"); // for visual studio
return 0;
}
double getTotalInventory(double unit_price, int number_of_jars)
{
return unit_price * number_of_jars;
}