We’ve been updating and extending the inventory project to use files and to use arrays. Now it’s time to put it all together.
I will give you a file inventory.txt
File Contents:
Red delicious apples
1.00 25 6 8 10
Assorted bouquets
4.00 50 10 10 0
Camembert cheese
2.00 25 10 12 4
END
The file will have the following specifications:
Each record consists of two lines.
The first line contains the name of the product, which may be more than one word long.
The second line contains the wholesale price as a decimal, the markup as a percentage, and three quantities, for counter, shelf, and warehouse.
If the first line of a record contains the word END all by itself, that is the end of the file.
Your task is to read each record into the program, as a string for the name, and numbers for the price and markup, and as an array of numbers for the quantities.
Then calculate the total profit for each item: (wholesale price) * (markup, converted into a decimal) * (total quantity).
Print out a report on one line for each item: Name, total profit.
Format the output nicely: names should fit in 30 characters and the prices should line up and should have two decimal places.
Hints:
You’ll want to use getline to read multi-word product names.
You’ll want to consider what kind of array to use for quantities – do you have fractional quantities or whole numbers?
You’ll want to review section 6.2 for how to produce a nicely formatted output.
PREVIOUS VERSION OF PROGRAM (Writing to a File):
#include
#include
#include
using namespace std;
int main ()
{
ofstream w;
int c=1;
string filename;
cout < "file="" name:="">
getline(cin, filename);
w.open(filename.c_str());
while(c==1){
string product_name;
cout < "product="" name:="">
getline(cin, product_name,'\n');
w < product_name=""><>
double wppi; //wholesale price per item
cout < "wholesale="" price:="">
cin >> wppi;
w < "wholesale="" price:="" "="">< wppi="">< "="">
int quanity;
cout < "quanity:="">
cin >> quanity;
w < "quanity:="" "="">< quanity="">< "="">
double total_price_paid;
total_price_paid = wppi * quanity;
double markup;
cout < "what="" is="" the="" markup?="">
cin >> markup;
double retail_price;
double anticipated_profit;
retail_price = wppi + markup;
anticipated_profit = total_price_paid - wppi;
w < "retail="" price:="" "="">< retail_price="">< "="" "="">< "anticipated="" profit:="" "="">< anticipated_profit=""><>
cout < "do="" you="" want="" to="" process="" another="" record?="" type="" 1="" for="" yes="" or="" 2="" for="" no."=""><>
cin >> c;
cin.ignore();
}
w.close();
return 0;
}
PREVIOUS VERSION OF THE PROGRAM (Reading from file):
#include
#include
#include
using namespace std;
int main ()
{
ifstream w;
string filename;
cout < "file="" name:="">
getline(cin, filename);
w.open(filename.c_str());
string line;
while(line!="END"){
getline(w,line);
cout<><>
}
w.close();
return 0;
}