Question

Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

Programming Assignment 1

Structures, arrays of structures, functions, header files, multiple code files

Program description:

Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory.

  1. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain more fields than will be read from the file:

Customer Name

Book Title

Number of Books requested for purchase

Number of Books actually purchased

Total Cost of purchase

Taxes

Declare the structure in a header file.

  1. You are to read a data file (bookList.txt) containing the inventory of books available for purchase. Created a structure to contain the information:

Book Title

Book Cost

Number in stock

Declare the structure in the header file

  1. Declare both structures in a header file. Declare array variables for both structures LOCALLY in main. Declare the array size for the customer data as 50. Declare the array size for the book data as 100.
  2. Using a function, open both input files and an output file for report and an error file to log errors. If any file fails to open, display a proper message to the customer and exit the program. Request the input and report file names from the user. The error file name may be a literal. Call the function from main.
  3. Using a function, read and store the customer data into the customer structure array. Read until end of file. Use a counter to count and track how many customers read. Pass the counter into any functions that loop through the structure array (do not use a global variable) and do not ‘hard code’ the actual number of rows. Using the same function, read and store the book data into the book structure array. Read until end of file. Use a counter to count and track how many books read. Pass the counter into any functions that loop through the structure array. Do not use a global variable and do not ‘hard code’ the number of rows. Call the function from main.
  4. Using a function, process the customer sales data as follows:

  1. For each customer, using the title of the book, search through the book array to find the book cost and the number in stock.
    1. If the book is found, make sure there are enough in stock to satisfy the purchase request.
      1. If there are enough in stock to satisfy the purchase request:
        • Update the number purchased field in the customer array.
        • Calculate the cost.
        • Calculate the taxes. Taxes are 5%.
        • Update the total cost field in the customer array. Total cost in the customer array is cost + taxes.
        • Update the taxes field in the customer array.
        • In the book structure array, adjust the number in stock for the book purchased. Ensure number in stock does not drop below zero.
      2. If there are not enough books in stock to satisfy the purchase request:
        • Do not make the sale.
        • Update the number purchased, tax and total cost fields in the customer array appropriately.
        • Write an error message to the error file.
    2. If the book is not found:
      1. Write an error in the error file.
      2. Update the number purchased, tax and total cost fields in the customer array appropriately.

  1. Using a function, output the customer data, which includes the updated number purchased, taxes and total cost, to the output console (cout) and to the output (report) file. Include a few spaces and output the updated book structure array to the output console (cout) and to the same report file. Monetary values must have 2 decimal places and be preceded by a dollar sign ($). Include appropriate report headings and appropriate field headings in the output. Output must be neat, readable and in columns.
  2. You must use a header file for the structure declarations and function prototypes. DO NOT CODE THE FUNCTIONS IN THE HEADER FILE.
  3. All functions must be in a code file (*.cpp) separate from main.
  4. Using a function, close all files prior to program end.
  5. Main should contain function calls and file open check only; no processing unless evaluating the return from a function call.
  6. All function code must be in a separate .cpp file (separate from main)
  7. Each function must have required documentation (pre and post conditions). Each submission must include a readme file (text or word file).
  8. See Readme and Commenting in Canvas for examples of the README file and commenting.

Test and evaluate calculations for accuracy. Points will be taken for inaccurate calculations, improper formatting, directions not followed.

Turn in code files(*.cpp); README file; and header files. You DO NOT need to submit the executable (*.exe). You MAY zip all the files and submit if you choose. You MUST include all the files indicated or points will be deducted.

____________________________________________________________

inside <bookList.txt>

C++ Programing: From Problem Analysis to Program Design
52.50
12
NOS4A2
19.00
100
Guitar Exercises
15.00
3
Harry Potter and The Half-Blood Prince
19.95
0
Beginning C# Object-Oriented Programming
75.00
9
Pro C# 5.0 and the 4.5 Framework
100.00
32
Social Psychology
80.00
11
Criminal Behavior A Psychological Approach
67.00
10
Essentials of Computer Organization and Architecture
92.00
7
Java Basics
78.00
4
____________________________________________

inside <customerList.txt>

John Williams
Guitar Exercises
1
Lisa Berry
Beginning C# Object-Oriented Programming
2
Ron Brown
Essentials of Computer Organization and Architecture
1
Jessey Smith
Harry Potter and The Half-Blood Prince
1
Harry Calahan
Criminal Behavior A Psychological Approach
2
Harley Masterfield
Powershell Scripting
1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Program

customerbook.h

#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
#define TAX .05
struct Customer {
   string customerName;
   string bookTitle;
   int purchaseReq;
   int actualPurchase;
   double purchaseCost;
   double taxes;
};
struct Book {
   string bookTitle;
   double bookCost;
   int stocksCnt;
};
//Function to check file open error
void fileOpen(ifstream& f1, ifstream& f2, ofstream& f3);
//Read file function
int readfile(ifstream& in,Book* books);
int readfile(ifstream& in, Customer* customers);
//Process function
void processing(Book* books, int cnt1, Customer* customers, int cnt2);
//Output function
void write(Book* books, int cnt1, Customer* customers, int cnt2,ofstream& out);

customerbook.cpp

#include "customerbook.h"
//Function to check file open error
void fileOpen(ifstream& f1, ifstream& f2, ofstream& f3) {
   if (!f1 || !f2 || !f3) {
       cout << "File not found!!!" << endl;
       exit(0);
   }
}
//Function to read file data into book array
int readfile(ifstream& in, Book* books) {
   int cnt = 0;
    while (!in.eof()) {
       in.ignore();
       getline(in, books[cnt].bookTitle);
       in >> books[cnt].bookCost;
       in >> books[cnt].stocksCnt;
       cnt++;
   }
   in.close();
   return cnt;
}
//Function to read file data into customer array
int readfile(ifstream& in,Customer* customers) {
   int cnt = 0;
   while (!in.eof()) {
       getline(in, customers[cnt].customerName);
       getline(in, customers[cnt].bookTitle);
       in >> customers[cnt].purchaseReq;
       in.ignore();
       cnt++;
   }
   in.close();
   return cnt;
}
//Function to process each customer
void processing(Book* books, int cnt1, Customer* customers, int cnt2) {
   for (int i = 0; i < cnt2; i++) {
       for (int j = 0; j < cnt1; j++) {
           if (books[j].bookTitle == customers[i].bookTitle) {
               if (customers[i].purchaseReq <= books[j].stocksCnt) {
                   customers[i].actualPurchase = customers[i].purchaseReq;
                   double cost = books[j].bookCost*customers[i].actualPurchase;
                   customers[i].taxes = cost * TAX;
                   customers[i].purchaseCost = cost + customers[i].taxes;
                   books[j].stocksCnt -= customers[i].actualPurchase;
               }
               else {
                   customers[i].actualPurchase =0;
                   customers[i].taxes = 0;
                   customers[i].purchaseCost =0;
                   cout << "Book not found!!" << endl;
               }
           }
       }
   }
}
//Write result onto console and file
void write(Book* books, int cnt1, Customer* customers, int cnt2, ofstream& out) {
   for (int i = 0; i < cnt2; i++) {
       cout <<fixed<< setprecision(2) << customers[i].customerName << "\n" << customers[i].bookTitle << "\n"
           << customers[i].actualPurchase << "\n" << customers[i].taxes << "\n"
           << customers[i].purchaseCost << endl;
       out<<fixed<< setprecision(2) << customers[i].customerName << "\n" << customers[i].bookTitle << "\n"
           << customers[i].actualPurchase << "\n" << customers[i].taxes << "\n"
           << customers[i].purchaseCost << endl;
   }
   cout << endl;
   out << endl;
   for (int i = 0; i < cnt1; i++) {
       cout <<fixed<< setprecision(2) << books[i].bookTitle << "\n" << books[i].bookCost << "\n" << books[i].stocksCnt << endl;
       out <<fixed<< setprecision(2) << books[i].bookTitle << "\n" << books[i].bookCost << "\n" << books[i].stocksCnt << endl;
   }
   out.close();
}

main.cpp

#include "customerbook.h"
#define BOOK_SIZE 100
#define CUST_SIZE 50

int main()
{
   //Create arrays for customers and books
   Book books[BOOK_SIZE];
   Customer customers[CUST_SIZE];
   //File paths set
   ifstream in1("bookList.txt");
   ifstream in2("customerList.txt");
   ofstream out("reportList.txt");
   //File open error check
   fileOpen(in1, in2, out);
   //Read two files
   int bookCnt=readfile(in1, books);
   int custCnt=readfile(in2, customers);
   //Process file data
   processing(books, bookCnt, customers, custCnt);
   //Output
   write(books, bookCnt, customers, custCnt, out);
}

--------------------------------------------------------

Note:-

I assume you are expecting this way and also i didn't understand what you mean by the error file write.

Add a comment
Know the answer?
Add Answer to:
Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

  • C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains...

    C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains integer values against which we are searching. There will be no more than 100 of these. The LSTest.txt file contains a set of numbers that we are trying to locate within the standard data set. There will be no more than 50 of these. Read both files into two separate arrays. Your program should then close both input files. All subsequent processing will be...

  • Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

    Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the...

  • Part 1 The purpose of this part of the assignment is to give you practice in...

    Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 file...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • design a C program to control the stock of books of a small bookstore. Part 1:...

    design a C program to control the stock of books of a small bookstore. Part 1: for the cout, it should be printf(" "); should write like that When a client orders a book, the system should check if the bookstore has the book in stock. If the bookstore keeps the title and there is enough stock for the order, the system should inform the price per copy to the client, as well as the total price of the purchase....

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • 8. (15 marks) Write a complete C program, which uses an array of structures and two...

    8. (15 marks) Write a complete C program, which uses an array of structures and two user defined functions. The program deals with a library database. The program will ask the user to input required information about each book and store it in a structure in a user defined function called get info. The second user defined function display_info will display database information on the screen. The output should look as follows: Book Title Book ld 123456 C Programming 10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT