(a) When you write your program, you will have three files: a declarations (head
ID: 3529815 • Letter: #
Question
(a) When you write your program, you will have three files: a declarations (header) file (call it invoices.h), a driver file (main.cpp) and a definition file (call it invoices.cpp). If it helps keep things straight, you may wish to do the whole program in one file, then split it up into separate files. (b) In the declarations file you will have your include statements and the declarations of your read and write functions. In the driver file you will include the header file, declare your variables and objects and call the read and write functions until the read file is empty; then close your files. In the definitions file you will again include the header file; in the filread() function you will read the data from the data file (see form below) and also return either true (good read) or false (no data); in the filwrite() function you will write to the file the required output in the specified form; it doesn't return anything. (c) You will need to pass the reference to the input file object to the filread() function and the reference to the output file object to the filwrite() function. Reference parameters will be covered in class. (d) in filwrite(), the numerical values should be printed with a field width of 8 spaces and the real numbers should be printed with 2 decimal places in a field of 8 spaces. Call your output file: invoices.txt . (e) We will talk about file I/O and output formatting in class. (f) You will need to create your own input data file to test your program (call it orders.dat - it is simply a text file with the extension .dat). It should look something like: Jones, Inc. 01234 5432 12 4.95 Abercrombie Buggywhips 10234 4321 20 8.45 etc. This is a batch program (not an interactive one). (g) Don't forget your documentation. (h) The progra m will be due on Wednesday of the following wee k and should include the Dev-C++ project file and the .cpp's and .h files - the simple way is to compress the project directory and upload it.Explanation / Answer
//PLEASE RATE
//invoice.h
#ifndef INVOICE_H
#define INVOICE_H
#include<cstring>
#include<fstream>
bool filread(char[], int&,int&,int&,double&, ifstream &);
void filwrite(char[], int,int,int,double,double,ofstream &outfile);
#endif
////////////////////////////////////////////////////////////
//invoice.cpp
#include"invoice.h"
using namespace std;
bool filread(char company[], int &invoice_number, int &part_number, int &quantity, double &unit_price, ifstream &infile) {
infile.getline(company,256);
//if(company==NULL){
if(infile.fail()){
return false;
}
infile>>invoice_number>>part_number>>quantity>>unit_price;
infile.get();
return true ;
}
void filwrite(char company[], int invoice_number, int part_number, int quantity, double unit_price, double total_price, ofstream &outfile){
outfile<<setiosflags(ios::fixed)<<setprecision(2);
outfile<<"Invoice"<<endl<<left//write invoice heading
<<setw(20)<<"Buyer company:"<<setw(8)<<company<<endl//write buyer company name with label
<<setw(20)<<"Invoice number:"<<setw(8)<<invoice_number<<endl// write invoice number with label
<<setw(20)<<"Part number:"<<setw(8)<<part_number<<endl//write part_number with label
<<setw(20)<<"Quantity:"<<setw(8)<<quantity<<endl //write quantity with label
<<setw(20)<<"Unit Price:"<<setw(8)<<unit_price<<endl //write unit_price with label
<<setw(20)<<"Total Price:"<<setw(8)<<total_price<<endl<<endl;//write total_price with label
return ;
}
////////////////////////////////////////////////////////////
//main.cpp
#include<iostream>
#include<cstring>
#include<fstream>
#include<iomanip>
#include"invoices.h"
using namespace std;
int main(){
char company[256];
bool ret;
int invoice_number, part_number, quantity;
double unit_price, total_price;
ifstream infile;
ofstream outfile;
//open files
infile.open("orders.dat");
outfile.open("invoices.txt");
ret = filread(company, invoice_number, part_number, quantity, unit_price, infile);
while (ret==true){
total_price = quantity * unit_price;
filwrite(company, invoice_number, part_number, quantity, unit_price, total_price, outfile);
ret = filread(company, invoice_number, part_number, quantity, unit_price, infile);
}//end while
//close files
infile.close();
outfile.close();
return 0;
}//stop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.