Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a program that reads data from an unformatted, tab-delimited file, products.txt , performs some calculations on that data, and writes the resulting data to

Create a program that reads data from an unformatted, tab-delimited file, products.txtimage text in transcribed, performs some calculations on that data, and writes the resulting data to another file called product_list.txt, thats formatted and uses spaces to align the columns (see specification of this file shown below).

You may use g++ or an IDE to develop this program. In either case, the files you submit for grading must meet the condition described next.

Console

image text in transcribed

image text in transcribed

Specifications

  • The input file products.txt contains the product data shown above, which is not supposed to be modified. Note that the total number of lines of data in this file could be changed later by adding more or deleting some of them, meaning your program cannot assume it contains fixed number of lines of data.
  • The program should read the data from the products.txt file which should be placed in the directory where the program is loacted. It calculates the discount amount and list price from this data, and writes the result to a text file named product_list.txt in the same directory where the program is located. This data should be formatted and aligned using spaces as shown above. See p.168~9 for how to write formatted data to a text file.
  • The program uses a class called ProductFile to handle file I/O operations. This class must be implemented in two files, productFile.h (for all members declaration) and productFile.cpp (for all members definition).
  • ProductFile class has a private string member 'where' to store a file name.
  • ProductFile class provides three and only three public functions as its interface:
    1. a constructor to set a file name to its 'where' member.
    2. readFile(), whose main duty is to create an input file stream object to open the text file indicated by the 'where' member and return the stream object. If the file opens, it displays "Reading product data from ...... file." Otherwise, it displays "Could not find input file named ......." It is noted that this function does not close the stream object because it is going to be used by the next function.
    3. writeFile(), which accepts an input file stream object and an out file name. It displays "Writing product data to ...... file" to indicate it starts working on the file. The first task is to use the given stream object to read all three pieces of data of each product, including product code, list price, and discount %, then calculate discount amount and discounted price. Its second task is to write a title and header followed by five data each product to a text file whose name is specified as this function's 2nd parameter. Be sure this function closes both stream objects at the end.

For the purpose of this assignment, writeFile() should use the eof() function of the input file stream to determine if there are more data in the stream to extract. It must also use the fail(), clear() and ignore() functions to fix the stream and try again when the extraction fails because of bad data.

**See p.167 for a good pattern and code example of using these stream functions.

**You may want to prepare your own file to test this part of your program.

  • The program's main() is implemented in prog4.cpp file. This should be a very simple and small function because it only prints "Product List" when the program starts and "Bye!" when it finishes. In between them, it makes a ProductFile object and tells it the data file name is "products.txt". Then, it makes a file stream object and asks it to do readFile(). Finally, before prints "Bye!", it lets the stream object perform writeFile().
Product List Reading product data from products.txt file Writing product data to product_list.txt file Bye! The products.txt file (aligned with tabs) CODE strat les paul 1199 sg fg700s washburn 299.0 PRICE 699.5 DISC PCT 3 3 52 38 2517 489.99 The product list.txt file (aligned with spaces) CURRENT PRODUCT LIST discount discount amount your price code price percent 699.50 1199.00 2517.00 489.99 299.00 30 30 52 1308.84 1208.16 38 209.85 359.70 489.65 839.30 strat les_pau.l sg fg700s washburn 186.20 0.00 303.79 299.00 Product List Reading product data from products.txt file Writing product data to product_list.txt file Bye! The products.txt file (aligned with tabs) CODE strat les paul 1199 sg fg700s washburn 299.0 PRICE 699.5 DISC PCT 3 3 52 38 2517 489.99 The product list.txt file (aligned with spaces) CURRENT PRODUCT LIST discount discount amount your price code price percent 699.50 1199.00 2517.00 489.99 299.00 30 30 52 1308.84 1208.16 38 209.85 359.70 489.65 839.30 strat les_pau.l sg fg700s washburn 186.20 0.00 303.79 299.00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions