Prototypes for your user defined functions are: 1. void openFile (ifstream&, string); 2. void readFile (ifstream&, PurchaseType[]); 3. void printPurchaseData (PurchaseType [], int);

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter11: More Object-oriented Programming Concepts
Section: Chapter Questions
Problem 2PE
icon
Related questions
Question

Please write in C++. Please look at the example and the example output.

Introduction
In this lab you will practice using basic input/output, structs, and arrays of structs. Be sure to read this lab thoroughly, especially
the Hand-in Procedure
Lab
To start your lab you will need a struct to read data into. The struct definition should be placed above main () and below
using namespace std. The struct should be called PurchaseType and contain the following fields:
1. string customerID
2. string productName
double price
3.
4. int qtyPurchased
5. double taxRate
Write a complete program that:
1.
Uses a user defined function:
void openFile(ifstream& iFile, string prompt)
Which gets the name of a file from the user and opens it into iFile by:
a. Prompting the user for a filename using prompt and reading the filename into a string.
b. Opening the file name obtained in (1a) into iFile, and verifying that it opened. If the file does not open the
program should output an error message and continue from (1a) until a valid file name is entered. See the
example output for error formatting.
i.
The file contains strings, integers, and doubles with 5 values per line with 10 lines of data in the file.
Each line of data represents a different item purchased by a customer.
2. Creates an array of instances of the PurchaseType struct to hold the data from the file obtained in (1).
3. Uses a user defined function:
void readFile(ifstream& iFile, PurchaseType purchases[])
Which reads the data from iFile into purchases by:
iFile contains rows of data which correspond to the elements of the PurchaseType struct. Iterate through
iFile inserting the rows of data, using str.find () str.substr(), into purchases until eof is hit.
а.
4. Uses a user defined function:
void printPurchaseData (PurchaseType purchases [], int size)
Which prints the data contained in purchases by:
a. Using iomanip to nicely output all the data in a tabular format. You will need to calculate the total price for
each product purchased by multiplying the price of each product by the quantity purchased, and then
multiplying by the (tax rate + 1). See the example output for formatting.
b. Keeping track of each total found and outputting the average total at the end of the table.
c. Keeping track of the count of unique customers and outputting the total unique customers at the end of the
table.
main () should only contain variable declarations and function calls (to your user-defined functions). All data must be read
into an array of PurchaseType structs first, then the required output. You must write all of the functions in this lab, failing to
do so will result in you receiving a 0 for this lab. Failure to use an array of structs will also result in a 0 for this lab. See
the example output for examples and formatting.
Prototypes for your user defined functions are:
1. void openFile (ifstream&, string);
2. void readFile(ifstream&, PurchaseType[]);
3. void printPurchaseData (PurchaseType[], int);
Transcribed Image Text:Introduction In this lab you will practice using basic input/output, structs, and arrays of structs. Be sure to read this lab thoroughly, especially the Hand-in Procedure Lab To start your lab you will need a struct to read data into. The struct definition should be placed above main () and below using namespace std. The struct should be called PurchaseType and contain the following fields: 1. string customerID 2. string productName double price 3. 4. int qtyPurchased 5. double taxRate Write a complete program that: 1. Uses a user defined function: void openFile(ifstream& iFile, string prompt) Which gets the name of a file from the user and opens it into iFile by: a. Prompting the user for a filename using prompt and reading the filename into a string. b. Opening the file name obtained in (1a) into iFile, and verifying that it opened. If the file does not open the program should output an error message and continue from (1a) until a valid file name is entered. See the example output for error formatting. i. The file contains strings, integers, and doubles with 5 values per line with 10 lines of data in the file. Each line of data represents a different item purchased by a customer. 2. Creates an array of instances of the PurchaseType struct to hold the data from the file obtained in (1). 3. Uses a user defined function: void readFile(ifstream& iFile, PurchaseType purchases[]) Which reads the data from iFile into purchases by: iFile contains rows of data which correspond to the elements of the PurchaseType struct. Iterate through iFile inserting the rows of data, using str.find () str.substr(), into purchases until eof is hit. а. 4. Uses a user defined function: void printPurchaseData (PurchaseType purchases [], int size) Which prints the data contained in purchases by: a. Using iomanip to nicely output all the data in a tabular format. You will need to calculate the total price for each product purchased by multiplying the price of each product by the quantity purchased, and then multiplying by the (tax rate + 1). See the example output for formatting. b. Keeping track of each total found and outputting the average total at the end of the table. c. Keeping track of the count of unique customers and outputting the total unique customers at the end of the table. main () should only contain variable declarations and function calls (to your user-defined functions). All data must be read into an array of PurchaseType structs first, then the required output. You must write all of the functions in this lab, failing to do so will result in you receiving a 0 for this lab. Failure to use an array of structs will also result in a 0 for this lab. See the example output for examples and formatting. Prototypes for your user defined functions are: 1. void openFile (ifstream&, string); 2. void readFile(ifstream&, PurchaseType[]); 3. void printPurchaseData (PurchaseType[], int);
Example 3
Alexs-iMac:labllb alex$ g++ -Wall -pedantic -Werror main.cpp
Alexs-iMac:lab11b alex$ ./a.out
Enter file name
**wrong
Error: Invalid File
Enter file name
**incorrect.csv
Error: Invalid File
Enter file name
**invalid.txt
Error: Invalid File
Enter file name
**err
Error: Invalid File
Enter file name
**purchases1.csv
| Customer ID | Product Name | Price | Quantity | Tax Rate | Total
shirt | 10.00 |
pants | 15.95 |
jacket | 24.49 |
blouse | 19.99 |
A134CX2 I
1 |
2 1
1 |
1 |
1 |
3 |
8.25 |
10.82 |
A134CX2 |
8.25 |
34.53 |
A134CX2 |
8.25 |
26.51 |
4J3712I |
8.25 |
21.64 |
pants | 14.98 |
mittens I
4J37121 |
8.25 |
16.22 |
4J37121 |
9.99 |
8.25 |
32.44 |
4J3712I |
socks | 10.98 |
3 |
35.66 |
8.25 |
7.15 |
B834621 |
gloves |
9.99 |
2 |
21.41 |
cap | 24.99 |
earrings | 49.99 |
B834621 |
3 |
7.15 |
80.33 |
27G8ZZY I
1 |
8.25 |
54.11 |
Unique users:
Average total: $33.37
4
Transcribed Image Text:Example 3 Alexs-iMac:labllb alex$ g++ -Wall -pedantic -Werror main.cpp Alexs-iMac:lab11b alex$ ./a.out Enter file name **wrong Error: Invalid File Enter file name **incorrect.csv Error: Invalid File Enter file name **invalid.txt Error: Invalid File Enter file name **err Error: Invalid File Enter file name **purchases1.csv | Customer ID | Product Name | Price | Quantity | Tax Rate | Total shirt | 10.00 | pants | 15.95 | jacket | 24.49 | blouse | 19.99 | A134CX2 I 1 | 2 1 1 | 1 | 1 | 3 | 8.25 | 10.82 | A134CX2 | 8.25 | 34.53 | A134CX2 | 8.25 | 26.51 | 4J3712I | 8.25 | 21.64 | pants | 14.98 | mittens I 4J37121 | 8.25 | 16.22 | 4J37121 | 9.99 | 8.25 | 32.44 | 4J3712I | socks | 10.98 | 3 | 35.66 | 8.25 | 7.15 | B834621 | gloves | 9.99 | 2 | 21.41 | cap | 24.99 | earrings | 49.99 | B834621 | 3 | 7.15 | 80.33 | 27G8ZZY I 1 | 8.25 | 54.11 | Unique users: Average total: $33.37 4
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Header Files
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage