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.
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.
Book Title
Book Cost
Number in stock
Declare the structure in the header file
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
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.
Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...
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 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 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 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 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 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...
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: 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 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 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...