Write a C++ program that processes orders for our company Widgets and More. Your program will read the product code and quantity for all the products in an order from a file, order.txt. You will be given a sample order.txt with the starter code. There are two items on each line of the file the first is a letter that represents the product and the second is an integer representing the number bought. Based on the product and quantity, you will calculate the total for the order. Then you will add 17% tax and 14.95 shipping costs to get the grand total for the order. You will print the order with the tax and shipping as shown below.
The unit costs for each product are listed below:
Product | Unit Price |
A | 17.46 |
B | 10.13 |
C | 2.11 |
D | 23.13 |
E | 74.56 |
F | 1.11 |
G | 9.34 |
H | 3.45 |
here is the starter code. PLEASE ADD THE STARTER CODE TO THIS PROGRAM. I HAVE TO USE IT. thank you.
PLEASE USE DOCUMENTATION FOR EACH STEP. I WANT TO KNOW UR THOUGHT PROCESS SO I BETTER UNDERSTAND HOW TO CREATE THIS PROGRAM MYSELF.
thank you so much for your help!!!!!!
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//input file
//Setting up the output table
cout << "Thank You! Your order is summarized
below:" << endl;
cout << setw(52) << setfill('-') <<
"-" << endl;
cout << setfill(' ');
cout << "| " << setw(15) << left
<< "Product" << "| " << setw(15) <<
"Quantity" << "| " << setw(15) << "Line Total"
<< "|"<< endl;
cout << setw(52) << setfill('-') <<
"-" << endl;
cout << setfill(' ');
return 0;
}
Attaching the well commented code snippet for the solution. Took order.txt file as input in which each line contains item name and quantity. Will attach test input and test output after the code snippet.
#include<bits/stdc++.h> //this header file includes all the necessary header files for moderate c++ and competetive coding.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#define endl "\n" //try to use "\n" instead of endl as endl always flushes the output and your program may take more time than usual.
using namespace std;
int main()
{
//input file
fstream order; //for taking input from order.txt file
string temp, file_name; //initialized temp for word to word extraction from order.txt file and file_name for specifying file.
double price[8] = {17.46 , 10.13 , 2.11 , 23.13 , 74.56 , 1.11 , 9.34 , 3.45};//price array will contain the price of all the items from A to H , 0 index representing A and so on.
file_name = "order.txt";
order.open(file_name.c_str());//for opening file
int count[8] = {0};// for storing the quantity of an item from order.txt file.
int i=0;// for identifying whether it is item name or item quantity. as the file will contain 1st word as item 2nd as quantity , then again 3rd as item and so on.Switing the value of i in the while loop.
char item;
while(order >> temp)//while loop for extracting word by word in temp variable.
{
if(i == 0)//if word is a item
{
item = temp[0];//item is stored in item variable, for example A,B,etc.
i = 1;
}
else //if word is quantity.
{
stringstream test(temp);//for converting quantity form string to integer.{you can also use stoi() function.
int quantity = 0;
test >> quantity;
count[(int)(item - 'A')] += quantity;//updating the count array with quantity of item.
i = 0;
}
}
double total = 0;
//Setting up the output table
cout << "Thank You! Your order is summarized below:" << endl;
cout << setw(52) << setfill('-') << "-" << endl;
cout << setfill(' ');
cout << "| " << setw(15) << left << "Product" << "| " << setw(15) << "Quantity" << "| " << setw(15) << "Line Total" << "|"<< endl;
cout << setw(52) << setfill(' ') << "-" << endl;
for(int i=0;i<8;i++)//This loop calculates the total price and prints item with quantity as output table.
{
if(count[i] != 0)
{
total += price[i]*count[i];
cout << "| " << setw(15) << left << (char)(65 + i) << "| " << setw(15) << count[i] << "| " << setw(15) << price[i]*count[i] << "|"<< endl;
}
}
cout<<endl<<endl;
//Price calculation
cout<<"Total = "<<total<<endl;
cout<<"Total(including 17% tax) = "<<total + 0.17*total<<endl;
cout<<"Grand Total(including tax and shipping) = "<<total + 0.17*total + 14.95<<endl;
cout << setfill(' ');
return 0;
}
Thankyou
Get Answers For Free
Most questions answered within 1 hours.