In: Computer Science
C++ Pie bakery
our bakery makes 8 kinds of pie, and receives orders for these pies from on-line buyers. The program you’ll write for this lab knows the price of each pie, so as each new order is received, it’s possible to know how much money the bakery should be receiving.
The program must read in lines of input which have two fields: a pie type and a quantity. With this, the program needs to update the quantity of that type of pie, adding the new order to those already received. You already have code that you’ve written for previous labs that uses getline() and splits the line read in into two separate parts: you get to re-use that here, since you have the same work to do – breaking an input string into two substrings, the first is the name of a kind of pie string and the second is the quantity int.
The program you are writing would be part of a larger system used by the bakery to track and schedule its baking. That’s good for you because it means that the input your program reads is guaranteed not to have bogus order numbers or pie types. A sample input line looks like:
raspberry 10
When the program reaches the end of input it prints out, for each type of pie, the quantity ordered and the total price of that number of those pies. After that it prints out the grand total of all orders (total number of pies, total price).
One problem you face is mapping pie types as strings (how you read them in) to the int index of the array element holding the struct for that pie type. For example, you might read in ”apple” and need to figure out that it’s in array element 3. You must use a function to handle this.
To make life a bit easier, you should initialize an array of struct with these pie types and prices:
peach lemon pumpkin blueberry apple raspberry strawberry cherry
$3.75 $4.50 $5.75 $4.25 $3.70 $6.35 $5.65 $5.20
Of course, you’ll need more than just these two fields in your
struct.
A sample run with 100 individual pie orders might produce output
like this.
Orders for peach: Orders for lemon: Orders for pumpkin: Orders for blueberry: Orders for apple: Orders for raspberry: Orders for strawberry: Orders for cherry:
115 value is $ 431.25 128 value is $ 576.00 75 value is $ 431.25 75 value is $ 318.75 71 value is $ 262.70 89 value is $ 565.15 122 value is $ 689.30 53 value is $ 275.60
Grand total: 728 pies for $3550.00
You can test your program with data you type in, or on a test data file you create.
#include <iostream>
using namespace std;
int main()
{
string pie_types [] = {"peach", "lemon", "pumpkin", "blueberry", "apple" ,"raspberry", "strawberry", "cherry"};
float price[] = {3.75 ,4.50 ,5.75 ,4.25 ,3.70 ,6.35, 5.65, 5.20};
int n;
float grand_total = 0;
int number_of_items[8];
int total_item = 0;
for(int i = 0;i<8;i++)
{
cout<<"Enter "<<pie_types[i]<<" quantity "; // Enter quantity for each item
cin>>number_of_items[i]; // Store quantity in number of items array
total_item += number_of_items[i]; // Total number of items
}
for(int i = 0;i<8;i++)
{
cout<<"Orders for "<<pie_types[i]<<" : "<<"$"<<(price[i]*number_of_items[i])<<endl; // total cost of each item
grand_total += price[i]*number_of_items[i];
}
cout <<"Grand Total: " <<total_item <<" Pies for "<<"$"<<grand_total; // Grand total of all the items
return 0;
}