CSC 201 Exam 1 Chapters 2-5 CONTINUING the program segment written in part a, ad
ID: 3903316 • Letter: C
Question
CSC 201 Exam 1 Chapters 2-5 CONTINUING the program segment written in part a, add the following. You may reuse any variables and actions defined in part a, i.e., you do NOT have to: rewrite header file declarations (except in extra credit) or main, open the file, place variables in the proper place in the previous program etc. Define any new variables or add any new header tiles in the code segment below. b. Add variable declarations to read a data file that contains the following (purchase -numbe name and cost): r of items, 1 Toothpaste 2 Bread 1 Cheese 5.50 (each) 2.25 (each) 7.50 (each) 1. Read the data from the open data file (from part a) into the proper variables 2. Add the total value of the groceries 3. Calculate 5% tax 4. Calculate (and store) total cost of the purchase, tax included 5. Output a message giving the total cost of the purchase, the tax, and what was purchased, i.e number of items and name. Ensure the output contains an informative message, not just the items and the costs. Extra credit (2 points if correct): Add the proper headers and use manipulators to properly output the results in a readable format with 2 decimal places for all monetary values 6.Explanation / Answer
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
ifstream fin("input.txt");
if (!fin){
cout << "Error opening file ";
return 0;
}
int q[100];
string item[100];
double cost[100];
double totalPrice[100];
int count = 0;
while(fin >> q[count] >> item[count] >> cost[count]){
count++;
}
for (int i = 0; i<count; i++){
totalPrice[i] = q[i] * cost[i];
}
double sum = 0;
for (int i = 0; i<count; i++){
sum = sum + totalPrice[i];
}
double tax = 0.05 * sum;
cout << setw(15) << left << "Quantity" << setw(15) << "Item name" << setw(15) << "Price/item" << setw(15) << "Price" << endl;
for (int i = 0; i<count; i++) {
cout << setw(15) << left << q[i] << setw(15) << item[i] << setw(15) << cost[i] << setw(15) << totalPrice[i] << endl;
}
cout << "Total Items:" << count << endl;
cout << "Total Purchase $" << sum << endl;
}
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
cout << "Enter a file name:";
string name;
cin >> name;
ifstream fin(name.c_str());
if (!fin){
cout << "Error opening file ";
return 0;
}
}