Intermediate Business Programming with C++

Open table as spreadsheet

Program

Demonstrates

append.cpp

Demonstrates output by appending data to a diskfile.

datain.cpp

Similar to textin.cpp below but uses variables of different data types for input.

dataout.cpp

Similar to textout.cpp below but uses variables of different data types for output.

inputsales.cpp

Shows the input of an object.

inputSalesArray.cpp

Shows file input of an array of objects.

outputsales.cpp

Shows the output of an object

outputSalesArray.cpp

Shows file output of an array of objects

salsimnycount.cpp

Demonstrates how to count the number of records in a binary file

salesin.cpp

Shows file input of a class object which is an array.

salesout.cpp

Shows file output of a class object whichis an array.

salsimny.cpp

Shows file input of multiple class objects which are arrays.

salsimnyeof.cpp

Shows file input of multiple class objects which are arrays like the example above but uses the function: eof().

salsomny.cpp

Shows file output of multiple class objects which are arrays.

salsimnysee.cpp

Demonstrates how to count the number of records in a binary file

textin.cpp

Shows how to input text files a character at a time using get()

textout.cpp

Shows how text files output

truncate.cpp

Demonstrates output by truncating the data in a disk file.

  • bookInventory.h

  • bookInventory.cpp

  • theNewFunctions.h

  • doBinaryInventory.cpp

These files provide a maintenance example of the following files by changing from text file I/O to binary file I.O for objects of a class.

  • bookInventory.h

  • bookInventory.cpp

  • theFunctions.h

  • doInventory.cpp

  • bookInventory.gif

  • sc_doInventory.gif

  • pc_doInventory.txt

These files are an example of text file I./O with objects.

// program_id append.cpp // author don voils // date written 3/25/2006 // // description This program demonstrates output // by appending data to a diskfile. // #include<fstream> #include<iostream> using namespace std; void main() { float theHourlyRate = 125.50; int hoursWorked = 45; ofstream outfile("testin2.txt",ios::app); if(!outfile) { cout << "File did not open." << endl << endl; exit(1); } outfile << "George" << endl; outfile << "Thomas" << endl; outfile << "Accountant" << endl; outfile << theHourlyRate << endl; outfile << hoursWorked << endl; } // program_id bookInventory.gif // written_by don voils // date_written 3/2/2007 // description The graphic below is the UML chart // for the class: bookInventory. //

// program_id bookInventory.cpp // written_by don voils // date_written 5/25/2006 // description This file contains the method definitions // for the class bookInventory. // #include "bookInventory.h" // The following methods are initilizing and // access methods for each of the attributes // of the class bookInventory. // void bookInventory::setbookName(string theName) { bookName = theName; } string bookInventory::getbookName() { return bookName; } void bookInventory::setISBN(string theISBN) { ISBN = theISBN; } string bookInventory::getISBN() { return ISBN; } void bookInventory::setyearPublished(string theYear) { yearPublished = theYear; } string bookInventory::getyearPublished() { return yearPublished; } void bookInventory::setnumberOnHand(string theNumber) { numberOnHand = theNumber; } string bookInventory::getnumberOnHand() { return numberOnHand; } void bookInventory::setcostBook(string theCost) { costBook = theCost; } string bookInventory::getcostBook() { return costBook; } void bookInventory::setpriceBook(string thePrice) { priceBook = thePrice; } string bookInventory::getpriceBook() { return priceBook; } void bookInventory::setbookAuthors(int theNumber,string theName) { bookAuthors[theNumber] = theName; } string bookInventory::getbookAuthors(int theNumber) { return bookAuthors[theNumber]; } // program_id bookInventory.h // written-by Don Voils // date_written 5/25/2006 // // Description: This header contains the definition // of the class bookInventory. // #include<iostream> #include<string> using namespace std; #ifndef INVENTORY #define INVENTORY // Although some of the attributes would normally // be numeric rather than strings, string data // type was chosen because of the way C++ handles // file I/O. // class bookInventory { private: string bookName; string ISBN; string yearPublished; string numberOnHand; string costBook; string priceBook; string bookAuthors[4]; public: void setbookName(string theName); string getbookName(); void setISBN(string theISBN); string getISBN(); void setyearPublished(string theYear); string getyearPublished(); void setnumberOnHand(string theNumber); string getnumberOnHand(); void setcostBook(string theCost); string getcostBook(); void setpriceBook(string thePrice); string getpriceBook(); void setbookAuthors(int theNumber,string theName); string getbookAuthors(int theNumber); }; #endif // program_id datain.cpp // author don voils // date written 3/25/2006 // // description This program demonstrates output // of strings or numbers to a diskfile // in ASCII text format. // #include<fstream> #include<iostream> #include<string> #include<iomanip> using namespace std; void main() { float theHourlyRate; int hoursWorked; string firstName; string lastName; string jobType; ifstream inFile("testing2.txt"); if(!inFile) { cout << "File did not open." << endl << endl; exit(1); } inFile >> firstName; inFile >> lastName; inFile >> jobType; inFile >> theHourlyRate; inFile >> hoursWorked; cout << fixed << setprecision(2); cout << endl << endl << setw(15) << left << "Employee: " << firstName << " " << lastName << endl << setw(15) << "Job Type: " << jobType << endl << setw(15) << "Hourly Rate: $" << theHourlyRate << endl << setw(15) << "Hours Worked: " << hoursWorked << endl << setw(15) << "Weekly Salary: $" << theHourlyRate * hoursWorked << endl; cout << endl << endl << "Continue? "; char resp; cin >> resp; cout << endl << endl; } // program_id dataout.cpp // author don voils // date written 3/25/2006 // // description This program demonstrates output // of strings or numbers to a diskfile // in ASCII text format. // #include<fstream> #include<iostream> using namespace std; void main() { float theHourlyRate = 125.50; int hoursWorked = 45; ofstream outFile("testing2.txt"); if(!outFile) { cout << "File did not open." << endl << endl; exit(1); } outFile << "George" << endl; outFile << "Thomas" << endl; outFile << "Accountant" << endl; outFile << theHourlyRate << endl; outFile << hoursWorked << endl; cout << endl << endl << "Continue? "; char resp; cin >> resp; cout << endl << endl; } // program_id doBinaryInventory.cpp // written_by don voils // date_written 5/25/2006 // // Description This is the menu for a program that processes // the inventory for Barnes and Nelson BookStores. // This is a modification of the file: doInventory.cpp // by changing the header from theFunctions.h to // the header theBinaryFunctions.h. // #include<iostream> using namespace std; // The following header contains the definition of the // class bookInventory. // #include "bookInventory.h" // The following header contains the functions that are // the modules for the program. // #include "theNewFunctions.h" void main() { // Used to receive the input for the // menu selection. // char index; // Used to control the menu loop. // bool More = true; // Used to define and control the // array of inventory items. // const int numberItems = 500; // The follwing array contains the inventory // items that the program processes. // bookInventory inventory[numberItems]; // The following module loads the inventory from // disk and places it into the array of inventory // items. If there is no data in the array, then // the array is filled with blank data. // loadBinaryInventory(inventory, numberItems); do { clearScreen(); cout << " Inventory Modification" << endl << endl << endl << " 1. Enter New Records" << endl << endl << " 2. Display by ISBN" << endl << endl << " 3. Edit Records" << endl << endl << " 4. Delete Records" << endl << endl << " 5. Exit" << endl << endl << " Which? "; cin.get(index).get(); cout << endl << endl; // This is an example of late binding. The compiler does not specify // the location of the jump until the execution of the program. // switch(index) { case '1': // This option permits the entry of inventory // records into the inventory array. // enterNewRecords(inventory,numberItems); break; case '2': // This option permits the display of an // inventory record given the ISBN of the book. // ISBNDisplay(inventory,numberItems); break; case '3': // This option permits the editing of each // item of a specific book record given the // ISBN of the book. // editItems(inventory,numberItems); break; case '4': // This option permits the deleting of an // item of a specific book record given the // ISBN of the book. // deleteItem(inventory,numberItems); break; case '5': clearScreen(); cout << "Data is being stored to file." << endl << endl; // This option terminates the program but // saves all of the data to a file prior // to ending. // saveBinaryInventory(inventory,numberItems); // Used to end the menu loop. // More = false; break; } }while(More); clearScreen(); cout << "Data has been stored to file." << endl << endl; } // program_id doInventory.cpp // written_by don voils // date_written 5/25/2006 // // Description This is the menu for a program that processes // the inventory for Barnes and Nelson BookStores. // #include<iostream> using namespace std; // The following header contains the definition of the // class bookInventory. // #include "bookInventory.h" // The following header contains the functions that are // the modules for the program. // #include "theFunctions.h" void main() { // Used to receive the input for the // menu selection. // char index; // Used to control the menu loop. // bool More = true; // Used to define and control the // array of inventory items. // const int numberItems = 500; // The follwing array contains the inventory // items that the program processes. // bookInventory inventory[numberItems]; // The following module loads the inventory from // disk and places it into the array of inventory // items. If there is no data in the array, then // the array is filled with blank data. // loadInventory(inventory, numberItems); do { clearScreen(); cout << " Inventory Modification" << endl << endl << endl << " 1. Enter New Records" << endl << endl << " 2. Display by ISBN" << endl << endl << " 3. Edit Records" << endl << endl << " 4. Delete Records" << endl << endl << " 5. Exit" << endl << endl << " Which? "; cin.get(index).get(); cout << endl << endl; // This is an example of late binding. The compiler does not specify // the location of the jump until the execution of the program. // switch(index) { case '1': // This option permits the entry of inventory // records into the inventory array. // enterNewRecords(inventory,numberItems); break; case '2': // This option permits the display of an // inventory record given the ISBN of the book. // ISBNDisplay(inventory,numberItems); break; case '3': // This option permits the editing of each // item of a specific book record given the // ISBN of the book. // editItems(inventory,numberItems); break; case '4': // This option permits the deleting of an // item of a specific book record given the // ISBN of the book. // deleteItem(inventory,numberItems); break; case '5': clearScreen(); cout << "Data is being stored to file." << endl << endl; // This option terminates the program but // saves all of the data to a file prior // to ending. // saveInventory(inventory,numberItems); // Used to end the menu loop. // More = false; break; } }while(More); clearScreen(); cout << "Data has been stored to file." << endl << endl; } // program_id inputSales.cpp // author don voils // date written 11/25/2006 // // program description This program demonstrates file // input of a class object. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void setDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int getMonth(){return month;} int getDay(){return day;} int getYear(){return year;} }; class sales : public Date { private: float theSales; public: void setSales() { setDate(); cout << endl << endl <<"What is the sales? "; cin >> theSales ; } float getSales() { return theSales; } }; void main() { sales today; ifstream infile("sales.txt",ios::in | ios::binary); infile.read((char *) &today, sizeof(today)); cout << fixed << setprecision(2); cout << "The sales for " << today.getMonth() << "/" << today.getDay() << "/" << today.getYear() << endl << endl; cout << "$" << today.getSales() << endl << endl; cout << endl << endl << "Continue? (Y/N) "; char resp; cin.get(resp); cout << endl << endl; } // program_id inputSalesArray.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates file // input of an array of class objects. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void setDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int getMonth(){return month;} int getDay(){return day;} int getYear(){return year;} }; class sales : public Date { private: float theSales; public: void setSales() { setDate(); cout << endl << endl <<"What is the sales? "; cin >> theSales ; } float getSales() { return theSales; } }; void main() { sales today[3]; ifstream infile("arraySales.txt",ios::in | ios::binary); infile.read((char *) &today, sizeof(today)); cout << fixed << setprecision(2); for(int index=0;index<3;index++) { cout << endl << "Day " << index+1 << endl << endl; cout << today[index].getMonth() << "/" << today[index].getDay() <<"/" << today[index].getYear() << endl; cout << "Sales: $ " << today[index].getSales() << endl; } cout << endl << endl << "Continue? (Y/N) "; char resp; cin.get(resp).get(); cout << endl << endl; } // program_id outputSales.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates file // output of a class object. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void setDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int getMonth(){return month;} int getDay(){return day;} int getYear(){return year;} }; class sales : public Date { private: float theSales; public: void setSales() { setDate(); cout << endl << endl <<"What is the sales? "; cin >> theSales ; } float getSales() { return theSales; } }; void main() { sales today; ofstream infile("sales.txt",ios::out | ios::binary); today.setSales(); infile.write((char *) &today, sizeof(today)); cout << endl << endl << "Continue? (Y/N) "; char resp; cin.get(resp).get(resp); cout << endl << endl; } // program_id outputSalesArray.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates file // ouput of an array of class objects. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void setDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int getMonth(){return month;} int getDay(){return day;} int getYear(){return year;} }; class sales : public Date { private: float theSales; public: void setSales() { setDate(); cout << endl << endl <<"What is the sales? "; cin >> theSales ; } float getSales() { return theSales; } }; void main() { sales today[3]; ofstream outfile("arraySales.txt",ios::out | ios::binary); cout << fixed << setprecision(2); for(int index=0;index<3;index++) { cout << endl << "Day " << index+1 << endl << endl; today[index].setSales(); } outfile.write((char *) &today, sizeof(today)); cout << endl << endl << "Continue? (Y/N) "; char resp; cin.get(resp).get(); cout << endl << endl; } // program_id pc_doInventory.txt // written_by don voils // date_written 3/2/2006 // description This is the pseudo code for the program bookInventory.cpp // doInventory Set More = true; Set numberItems = 500; Process loadInventory(inventory[], numberItems); Do Process clearScreen(); Display " Inventory Modification" " 1. Enter New Records" " 2. Display by ISBN" " 3. Edit Records" " 4. Delete Records" " 5. Exit" << endl " Which? " Receive Choice CASE of Choice '1': Process enterNewRecords(inventory[],numberItems) '2': Process ISBNDisplay(inventory[],numberItems) '3': Process editItems(inventory[],numberItems) '4': Process deleteItem(inventory[],numberItems) '5': Process clearScreen(); Display "Data is being stored to file." Process saveInventory(inventory[],numberItems) Set More = false ENDCASE }while(More) Process clearScreen(); Display "Data has been stored to file." END clearScreen() FOR(loop_counter=0;loop_counter<=100;++loop_counter) Display carriageReturn ENDFOR RETURN loadInventory(inventory[],numberItems) Open inFile(a:\inventory.dat) IF(!inFile)THEN Set startString = "Start" Set aString = "****" FOR(index = 0; index < numberItems;++index) inventory[index].setbookName(startString) inventory[index].setISBN(aString) inventory[index].setyearPublished(aString) inventory[index].setnumberOnHand(aString) inventory[index].setcostBook(aString) inventory[index].setpriceBook(aString) inventory[index].setbookAuthors(0,aString) inventory[index].setbookAuthors(1,aString) inventory[index].setbookAuthors(2,aString) inventory[index].setbookAuthors(3,"end") ENDFOR ELSE FOR(index = 0;index<numberItems;++index) Receive aString inventory[index].setbookName(aString) Receive aString inventory[index].setISBN(aString) Receive aString inventory[index].setyearPublished(aString) Receive aString inventory[index].setnumberOnHand(aString) Receive aString inventory[index].setcostBook(aString) Receive aString inventory[index].setpriceBook(aString) Receive aString inventory[index].setbookAuthors(0,aString) Receive aString inventory[index].setbookAuthors(1,aString) Receive aString inventory[index].setbookAuthors(2,aString) Receive aString inventory[index].setbookAuthors(3,aString) ENDFOR ENDIF Close inFile RETURN enterNewRecords(inventory[],numberItems) { Set recordNumber = -1 FOR(index = 0; index < numberItems;++index) IF(inventory[index].getISBN() == "****")THEN recordNumber = index break ENDIF ENDFOR IF(recordNumber == -1)THEN Process clearScreen() Display "Error file is full. Continue? " Receive resp ELSE Process clearScreen() Request "What is the book name? " Receive aString inventory[index].setbookName(aString) Request "What is the ISBN? " Receive aString inventory[index].setISBN(aString) Request "What is the year the book was published? " Receive aString inventory[index].setyearPublished(aString) Request "How many of this book do we have on hand? " Receive aString inventory[index].setnumberOnHand(aString); Request "What was the cost of the book? " Receive aString inventory[index].setcostBook(aString) Request "What is the price of the book? " Receive aString inventory[index].setpriceBook(aString) Request "What is the name of the first author? " Receive aString inventory[index].setbookAuthors(0,aString) Request "What is the name of the second author? " Receive aString inventory[index].setbookAuthors(1,aString) Request "What is the name of the third author? " Receive aString inventory[index].setbookAuthors(2,aString) Request "What is the name of the fourth author? " Receive aString inventory[index].setbookAuthors(3,aString) ENDIF RETURN ISBNDisplay(bookInventory inventory[],int const &numberItems) Process clearScreen() Display"What is the ISBN? " Receive aString Set recordNumber = -1 FOR(index = 0; index < numberItems;++index) IF(inventory[index].getISBN() == aString)THEN Set recordNumber = index break ENDIF ENDFOR IF(recordNumber == -1)THEN Process clearScreen() Display "Error. There is no book with that ISBN. Continue? " Receive resp ELSE Process clearScreen() Display "The book name is: " << inventory[index].getbookName() Display "The ISBN is: " << inventory[index].getISBN() Display "The year the book was published: " Display inventory[index].getyearPublished() Display "The number of this book that we have on hand is: " Display inventory[index].getnumberOnHand() Display "The cost of the book was $: " Display inventory[index].getcostBook() Display "The total cost is: $" Display atof((inventory[index].getcostBook()).c_str())* atoi((inventory[index].getnumberOnHand()).c_str()) Display "The price of the book is: $" Display inventory[index].getpriceBook() Display "The total selling price is: $" Display atof((inventory[index].getpriceBook()).c_str())* atoi((inventory[index].getnumberOnHand()).c_str()) Display "The name of the first author is: " Display inventory[index].getbookAuthors(0) Display "The name of the second author is: " Display inventory[index].getbookAuthors(1) Display "The name of the third author is: " Display inventory[index].getbookAuthors(2) Display "The name of the fourth author? " Display inventory[index].getbookAuthors(3) Display "Continue? " Receive resp ENDIF RETURN editItems(inventory[],numberItems) Set recordNumber = -1 Process clearScreen() Display "What is the ISBN? " Receive aString FOR(int index = 0; index < numberItems;++index) IF(inventory[index].getISBN() == aString)THEN Set recordNumber = index break ENDIF ENDFOR IF(recordNumber == -1)THEN Process clearScreen() Display "Error!! There is no such book on record. Continue? " Receive resp ELSE Process clearScreen(); Display "The current data on that book is:" << endl << endl Display "The book name is: " << inventory[index].getbookName() Display "The ISBN is: " << inventory[index].getISBN() Display "The year the book was published: " Display inventory[index].getyearPublished() Display "The number of this book that we have on hand is: " Display inventory[index].getnumberOnHand() Display "The cost of the book was: $" Display inventory[index].getcostBook() Display "The price of the book is: $" Display inventory[index].getpriceBook() Display "The name of the first author is: " Display inventory[index].getbookAuthors(0) Display "The name of the second author is: " Display inventory[index].getbookAuthors(1) Display "The name of the third author is: " Display inventory[index].getbookAuthors(2) Display "The name of the fourth author? " Display inventory[index].getbookAuthors(3) << endl << endl Display "Continue? " Receive resp Display "What is the book name? " Receive aString inventory[index].setbookName(aString) Display "What is the ISBN? " Receive aString inventory[index].setISBN(aString) Display "What is the year the book was published? " Receive aString inventory[index].setyearPublished(aString) Display "How many of this book do we have on hand? " Receive aString inventory[index].setnumberOnHand(aString) Display "What was the cost of the book? " Receive aString inventory[index].setcostBook(aString) Display "What is the price of the book? " Receive aString inventory[index].setpriceBook(aString) Display "What is the name of the first author? " Receive aString inventory[index].setbookAuthors(0,aString) Display "What is the name of the second author? " Receive aString inventory[index].setbookAuthors(1,aString) Display "What is the name of the third author? " Receive aString inventory[index].setbookAuthors(2,aString) Display "What is the name of the fourth author? " ; Receive aString inventory[index].setbookAuthors(3,aString) Display "Continue? " Receive resp ENDIF RETURN deleteItem(bookInventory inventory[],int const &numberItems) Set recordNumber = -1 Process clearScreen(); Display "What is the ISBN of the book you want to delete? " Receive aString FOR(index = 0; index < numberItems;++index) IF(inventory[index].getISBN() == aString)THEN SetrecordNumber = index; break; ENDIF ENDFOR IF(recordNumber == -1)THEN Process clearScreen() Display "Error!! There is no such book on record. Continue? " Receive resp) ELSE Process clearScreen() Display "The current data on the book to be deleted is:" Display "The book name is: " << inventory[index].getbookName() Display "The ISBN is: " << inventory[index].getISBN() Display "The year the book was published: " Display inventory[index].getyearPublished() Display "The number of this book that we have on hand is: " Display inventory[index].getnumberOnHand() Display "The cost of the book was: $" Display inventory[index].getcostBook() Display "The price of the book is: $" Display inventory[index].getpriceBook() Display "The name of the first author is: " Display inventory[index].getbookAuthors(0) Display "The name of the second author is: " Display inventory[index].getbookAuthors(1) Display "The name of the third author is: " Display inventory[index].getbookAuthors(2) Display "The name of the fourth author? " Display inventory[index].getbookAuthors(3) Display "Continue to delete? (Y/N)" Receive resp) IF((resp=='Y') | (resp == 'y'))THEN Set startString = "Start"; Set aString = "****"; Set endString = "End" inventory[index].setbookName(startString) inventory[index].setISBN(aString) inventory[index].setyearPublished(aString) inventory[index].setnumberOnHand(aString) inventory[index].setcostBook(aString) inventory[index].setpriceBook(aString) inventory[index].setbookAuthors(0,aString) inventory[index].setbookAuthors(1,aString) inventory[index].setbookAuthors(2,aString) inventory[index].setbookAuthors(3,endString) ENDIF ENDIF RETURN saveInventory(inventory[],numberItems) OPEN outFile("a:\\inventory.dat"); FOR(index = 0; index < numberItems; ++index) Set aString = inventory[index].getbookName() Save aString to outFile Set aString = inventory[index].getISBN() Save aString to outFile Set aString = inventory[index].getyearPublished() Save aString to outFile Set aString = inventory[index].getnumberOnHand() Save aString to outFile Set aString = inventory[index].getcostBook() Save aString to outFile Set aString = inventory[index].getpriceBook() Save aString to outFile Set aString = inventory[index].getbookAuthors(0) Save aString to outFile Set aString = inventory[index].getbookAuthors(1) Save aString to outFile Set aString = inventory[index].getbookAuthors(2) Save aString to outFile Set aString = inventory[index].getbookAuthors(3) Save aString to outFile ENDFOR outFile.close(); RETURN // program_id salesin.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates file // input of a class object that is // an array. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void getDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int showMonth(){return month;} int showDay(){return day;} int showYear(){return year;} }; class Sales : public Date { private: float theSales[5]; public: void getSales() { for(int gl = 3; gl<=7;++gl) { char ch; do { cout << endl <<"What is the sales for GL # " << gl << "? "; cin >> theSales[gl-3]; cout << endl << "Correct? (Y/N) "; cin.get(ch).get(); }while(ch=='Y' || ch=='y'); } } void showAll() { float totalSales=0.00; for(int gl = 3; gl<=7;++gl) { cout << endl << "Sales GL #" << gl << ": $" << setprecision(2) << setw(10) << setiosflags(ios::fixed) <<theSales[gl-3]; totalSales +=theSales[gl-3]; } cout << endl << setw(24) << "----------" << endl << "Totals Sales $" << setprecision(2) << setw(10) << setiosflags(ios::fixed) << totalSales; } }; void main() { char resp; Sales today; ifstream infile("sales1.txt",ios::in | ios::binary); infile.read((char *) &today, sizeof(today)); cout << "The sales for " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << endl << endl; today.showAll(); cout << endl << endl << "Continue? (Y/N) "; cin.get(resp).get(); cout << endl << endl; } // program_id salesout.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates output of // a class object. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void getDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int showMonth(){return month;} int showDay(){return day;} int showYear(){return year;} }; class Sales : public Date { private: float theSales[5]; public: void getSales() { for(int gl = 3; gl<=7;++gl) { char ch; do { cout << endl <<"What is the sales for GL # " << gl << "? "; cin >> theSales[gl-3]; cout << endl << "Correct? (Y/N) "; cin.get(ch).get(ch); }while(ch!='Y' && ch!='y'); } } void showAll() { float totalSales=0.00; for(int gl = 3; gl<=7;++gl) { cout << endl << "Sales GL #" << gl << ": $" << setprecision(2) << setw(10) << setiosflags(ios::fixed) <<theSales[gl-3]; totalSales += theSales[gl-3]; } cout << endl << setw(23) << " ----------"; cout << endl << "Total Sales $" << setprecision(2) << setw(10) << setiosflags(ios::fixed) << totalSales << endl << endl; } }; void main() { Sales today; today.getDate(); today.getSales(); for(int count = 1; count <= 25;++count) cout << endl; cout << flush; cout << "The sales for " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << endl << endl; today.showAll(); ofstream outfile("sales1.txt",ios::out|ios::binary); outfile.write((char *) &today, sizeof(today)); } // program_id salsimny.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates input of // many class objects in an array. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void getDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int showMonth(){return month;} int showDay(){return day;} int showYear(){return year;} }; class Sales : public Date { private: float theSales[5]; public: void getSales() { for(int gl = 3; gl<=7;++gl) { char ch; do { cout << endl <<"What is the sales for GL # " << gl << "? "; cin >> theSales[gl-3]; cout << endl << "Correct? (Y/N) "; cin.get(ch).get(); }while(ch=='Y' || ch=='y'); } } void showAll() { float totalSales=0.00; for(int gl = 3; gl<=7;++gl) { cout << endl << "Sales GL #" << gl << ": " << setprecision(2) << setw(10) << setiosflags(ios::fixed) <<theSales[gl-3]; totalSales += theSales[gl-3]; } cout << endl << setw(23) << "----------"; cout << endl << "Totals Sales " << setprecision(2) << setw(10) << setiosflags(ios::fixed) << totalSales; } }; void main() { Sales today; int count; char resp='Y', ch; ifstream infile("sales3.txt", ios::in | ios::binary); if(!infile) { cout << "Error. File would not open."; exit(1); } infile.read((char *) &today, sizeof(today)); while((infile) && ((resp=='Y') || (resp=='y'))) { for(int count=1;count<=250;++count) cout << endl; cout << flush; cout << "The sales for " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << endl << endl; today.showAll(); cout << endl << endl << "Would you like to see another record? (Y/N)"; cin.get(resp).get(); if((resp=='Y') || (resp=='y')) infile.read((char *) &today, sizeof(today)); } if(!infile) cout << endl << "That is the last of the data. " << endl << endl; cout << endl << endl << "Continue? "; cin >> ch; cout << endl << endl; } // program_id salsimnycount.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates how to count // the number of records in a binary file. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void getDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int showMonth(){return month;} int showDay(){return day;} int showYear(){return year;} }; class Sales : public Date { private: float theSales[5]; public: void getSales() { for(int gl = 3; gl<=7;++gl) { char ch; do { cout << endl <<"What is the sales for GL # " << gl << "? "; cin >> theSales[gl-3]; cout << endl << "Correct? (Y/N) "; cin.get(ch).get(); }while(ch=='Y' || ch=='y'); } } void showAll() { float totalSales=0.00; for(int gl = 3; gl<=7;++gl) { cout << endl << "Sales GL #" << gl << ": " << setprecision(2) << setw(10) << setiosflags(ios::fixed) <<theSales[gl-3]; totalSales += theSales[gl-3]; } cout << endl << setw(23) << "----------"; cout << endl << "Totals Sales " << setprecision(2) << setw(10) << setiosflags(ios::fixed) << totalSales; } }; void main() { Sales today; int count; char resp='Y', ch; ifstream infile("sales3.txt", ios::in | ios::binary); if(!infile) { cout << "Error. File would not open."; exit(1); } infile.seekg(0,ios::end); int position = infile.tellg(); int number_records = position / sizeof(today); cout << "There are " << number_records << " recorded. " << endl << endl; infile.seekg(0,ios::beg); infile.read((char *) &today, sizeof(today)); while((infile) && ((resp=='Y') || (resp=='y'))) { for(count=1;count<=25;++count) cout << endl; cout << flush; cout << "The sales for " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << endl << endl; today.showAll(); cout << endl << endl << "Would you like to see another record? (Y/N)"; cin.get(resp).get(); if((resp=='Y') || (resp=='y')) infile.read((char *) &today, sizeof(today)); } if(!infile) cout << endl << "That is the last of the data. " << endl << endl; cout << endl << endl << "Continue? "; cin >> ch; cout << endl << endl; } // program_id salsimnyeof.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates input of // many class objects in an array using the // function eof(). // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void getDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int showMonth(){return month;} int showDay(){return day;} int showYear(){return year;} }; class Sales : public Date { private: float theSales[5]; public: void getSales() { for(int gl = 3; gl<=7;++gl) { char ch; do { cout << endl <<"What is the sales for GL # " << gl << "? "; cin >> theSales[gl-3]; cout << endl << "Correct? (Y/N) "; cin.get(ch).get(); }while(ch=='Y' || ch=='y'); } } void showAll() { float totalSales=0.00; for(int gl = 3; gl<=7;++gl) { cout << endl << "Sales GL #" << gl << ": " << setprecision(2) << setw(10) << setiosflags(ios::fixed) <<theSales[gl-3]; totalSales += theSales[gl-3]; } cout << endl << setw(23) << "----------"; cout << endl << "Totals Sales " << setprecision(2) << setw(10) << setiosflags(ios::fixed) << totalSales; } }; void main() { sales today; int count; char resp='Y', ch; ifstream infile("sales3.txt", ios::in | ios::binary); if(!infile) { cout << "Error. File would not open."; exit(1); } infile.read((char *) &today, sizeof(today)); while((!infile.eof()) && ((resp=='Y') || (resp=='y'))) { for(count=1;count<=250;++count) cout << endl; cout << flush; cout << "The sales for " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << endl << endl; today.showAll(); cout << endl << endl << "Would you like to see another record? (Y/N)"; cin.get(resp).get(); if((resp=='Y') || (resp=='y')) infile.read((char *) &today, sizeof(today)); } if(!infile) cout << endl << "That is the last of the data. " << endl << endl; cout << endl << endl << "Continue? "; cin >> ch; cout << endl << endl; } // program_id salsimnysee.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates how to count // the number of records in a binary file. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void getDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int showMonth(){return month;} int showDay(){return day;} int showYear(){return year;} }; class Sales : public Date { private: float theSales[5]; public: void getSales() { for(int gl = 3; gl<=7;++gl) { char ch; do { cout << endl <<"What is the sales for GL # " << gl << "? "; cin >> theSales[gl-3]; cout << endl << "Correct? (Y/N) "; cin.get(ch).get(); }while(ch=='Y' || ch=='y'); } } void showAll() { float totalSales=0.00; for(int gl = 3; gl<=7;++gl) { cout << endl << "Sales GL #" << gl << ": " << setprecision(2) << setw(10) << setiosflags(ios::fixed) <<theSales[gl-3]; totalSales += theSales[gl-3]; } cout << endl << setw(23) << "----------"; cout << endl << "Totals Sales " << setprecision(2) << setw(10) << setiosflags(ios::fixed) << totalSales; } }; void main() { Sales today; char resp='Y', ch; ifstream infile("sales3.txt", ios::in | ios::binary); if(!infile) { cout << "Error. File would not open."; exit(1); } infile.seekg(0,ios::end); int position = infile.tellg(); int numberRecords = position / sizeof(today); int seeRecord; cout << "There are " << numberRecords << " recorded. " << endl << endl; do { do { cout << endl << endl << "Which record do you want to see? "; cin >> seeRecord; if(seeRecord > numberRecords) cout << endl << "Error. That record is beyond the end of the file." << endl << "Try again." << endl << endl; }while(seeRecord > numberRecords); cout << endl << endl; infile.seekg((seeRecord-1)*sizeof(today),ios::beg); infile.read((char *) &today, sizeof(today)); cout << "The sales for " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << endl << endl; today.showAll(); cout << endl << endl << "Would you like to see another record? (Y/N)"; cin.get(resp).get(resp); }while((resp=='Y') || (resp=='y')); cout << endl << endl << "Continue? "; cin >> ch; cout << endl << endl; } // program_id salsomny.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates output of // many class objects. // #include<fstream> #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void getDate() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; } int showMonth(){return month;} int showDay(){return day;} int showYear(){return year;} }; class Sales : public Date { private: float dailySales[5]; public: void getSales() { for(int gl = 3; gl<=7;++gl) { char ch, resp; do { cout << endl <<"What is the sales for GL # " << gl << "? "; cin >> dailySales[gl-3]; cout << endl << "Correct? (Y/N) "; resp=' '; cin.get(ch).get(resp).get(); }while((resp!='Y') && (resp!='y')); } } void showAll() { float ttlSales=0.00; for(int gl = 3; gl<=7;++gl) { cout << endl << "Sales GL #" << gl << ": " << setprecision(2) << setw(10) << setiosflags(ios::fixed) <<dailySales[gl-3]; ttlSales += dailySales[gl-3]; } cout << endl << setw(23) << "----------"; cout << endl << "Totals Sales " << setprecision(2) << setw(10) << setiosflags(ios::fixed) << ttlSales; } }; void main() { Sales today; ofstream outfile("sales3.txt",ios::binary | ios::app); char resp; do { for(int count = 1; count <= 50;++count) cout << endl; cout << flush; today.getDate(); today.getSales(); for(int count = 1; count <= 250;++count) cout << endl; cout << flush; cout << "The sales for " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << endl << endl; today.showAll(); outfile.write((char *) &today, sizeof(today)); cout << endl << endl << "Store another day's sales? (Y/N) "; resp=' '; cin.get(resp).get(); }while((resp=='Y') || (resp=='y')); cout << endl << endl; } // program_id sc_doinventory.gif // written_by don voils // date_written 2/3/2007 // description This is the structure chart for the program doInventory.cpp //

// program_id textin.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates input of // strings and characters from a disk file // one character at a time. // #include<fstream> #include<iostream> using namespace std; void main() { char ch; ifstream inFile("testing.txt"); if(!inFile) { cout << "Error opening the file."; exit(1); } // // Look at the data file. Notice that it contains text. // Notice further that a number was sent the the file // yet it was stored as text. When it is read in, it appears // to be a number but it is text. Each character is set out // and retrieved as a single character. // inFile.get(ch); while(inFile) { cout << ch; inFile.get(ch); } cout << endl << endl; cout << "Continue? "; cin >> ch; cout << endl << endl; } // program_id textout.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates output // of strings or numbers to a diskfile // in ASCII text format. // #include<fstream> #include<iostream> using namespace std; void main() { char* text = "This is a test string."; int aNumber = 125; ofstream outFile("testing.txt"); if(!outFile) { cout << "File did not open." ; exit(1); } outFile << "This is a test of outputing text in line 1." << endl; outFile << "This is a test of outputing text in line 2." << endl; outFile << "This is a test of outputing text in line 3." << endl; outFile << "This is a test of outputing text in line 4." << endl; outFile << text << endl; outFile << "The number = " << aNumber; char ch; cout << endl << endl; cout << "Continue? "; cin >> ch; cout << endl << endl; } // program-id theFunctions.h // written-by Don Voils // date written 5/27/2006 // // Description This file contains the definition of the // function that are the modules for the // program doInventory.cpp. // #include<iostream> #include<string> #include<fstream> #include<iomanip> #include<cstdlib> using namespace std; // The defintion of the bookInventory class // must be included into the program. // #include"bookInventory.h" // This function is used to clear the screen for // a better user presentation. // void clearScreen() { for(int loop_counter=0;loop_counter<=250;++loop_counter) cout << endl; } // This function is used to load from file the book records // and then stores them into the inventory array. // void loadInventory(bookInventory inventory[],int const &numberItems) { // The data file that holds the inventory is opened // ifstream inFile("inventory.dat"); // If the file is empty, then it is filled with // empty data. // if(!inFile) { string startString = "Start"; string aString = "****"; //cout << "Starting." << endl; //char resp; //cin.get(resp).get(); for(int index = 0; index < numberItems;++index) { inventory[index].setbookName(startString); inventory[index].setISBN(aString); inventory[index].setyearPublished(aString); inventory[index].setnumberOnHand(aString); inventory[index].setcostBook(aString); inventory[index].setpriceBook(aString); inventory[index].setbookAuthors(0,aString); inventory[index].setbookAuthors(1,aString); inventory[index].setbookAuthors(2,aString); inventory[index].setbookAuthors(3,"end"); } } else { // If the file is not empty, it is input into // the array of inventory items array. // string aString; for(int index = 0;index<numberItems;++index) { getline(inFile,aString); inventory[index].setbookName(aString); getline(inFile,aString); inventory[index].setISBN(aString); getline(inFile,aString); inventory[index].setyearPublished(aString); getline(inFile,aString); inventory[index].setnumberOnHand(aString); getline(inFile,aString); inventory[index].setcostBook(aString); getline(inFile,aString); inventory[index].setpriceBook(aString); getline(inFile,aString); inventory[index].setbookAuthors(0,aString); getline(inFile,aString); inventory[index].setbookAuthors(1,aString); getline(inFile,aString); inventory[index].setbookAuthors(2,aString); getline(inFile,aString); inventory[index].setbookAuthors(3,aString); } } inFile.close(); } // This function permits the entry of a book records. It first // finds an empty record in the inventory array and then permits // the individual data items to be entered into the particular record. // void enterNewRecords(bookInventory inventory[],int const &numberItems) { // The inventory array is searched for an empty record. // int recordNumber = -1; for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == "****") { recordNumber = index; break; } } // If the inventory array has no more empty records, the // user is notified of that case. // if(recordNumber == -1) { clearScreen(); cout << "Error file is full. Continue? " << endl; char resp; cin.get(resp).get(); } else { // If the inventory array has an empty record, // the data is entered into that record. // clearScreen(); string aString; cout << "What is the book name? "; getline(cin,aString); inventory[index].setbookName(aString); cout << endl << "What is the ISBN? "; getline(cin,aString); inventory[index].setISBN(aString); cout << endl << "What is the year the book was published? "; getline(cin,aString); inventory[index].setyearPublished(aString); cout << endl << "How many of this book do we have on hand? "; getline(cin,aString); inventory[index].setnumberOnHand(aString); cout << endl << "What was the cost of the book? "; getline(cin,aString); inventory[index].setcostBook(aString); cout << endl << "What is the price of the book? "; getline(cin,aString); inventory[index].setpriceBook(aString); cout << endl << "What is the name of the first author? "; getline(cin,aString); inventory[index].setbookAuthors(0,aString); cout << endl << "What is the name of the second author? "; getline(cin,aString); inventory[index].setbookAuthors(1,aString); cout << endl << "What is the name of the third author? "; getline(cin,aString); inventory[index].setbookAuthors(2,aString); cout << endl << "What is the name of the fourth author? "; getline(cin,aString); inventory[index].setbookAuthors(3,aString); } } // This function displays the data values of a particular record // given the ISBN of the book. // void ISBNDisplay(bookInventory inventory[],int const &numberItems) { string aString; clearScreen(); // The ISBN of the record to be displayed is requested. // cout << "What is the ISBN? "; getline(cin,aString); // The manipulators for output is set up. // cout << setprecision(2); cout << fixed; // The inventory array is searched to determine // whether a record exists with the requested // ISBN. // int recordNumber = -1; for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == aString) { recordNumber = index; break; } } // If no record exists for the requested ISBN, // an error message is returned. // if(recordNumber == -1) { clearScreen(); cout << "Error. There is no book with that ISBN. Continue? "; char resp; cin.get(resp).get(); } else { // When the record for the requested ISBN book exists, // the data for the book is displayed. // clearScreen(); cout << "The book name is: " << inventory[index].getbookName() << endl << "The ISBN is: " << inventory[index].getISBN() << endl << "The year the book was published: " << inventory[index].getyearPublished() << endl << "The number of this book that we have on hand is: " << inventory[index].getnumberOnHand() << endl << "The cost of the book was $: " << inventory[index].getcostBook() << endl << "The total cost is: $" << atof((inventory[index].getcostBook()).c_str())* atoi((inventory[index].getnumberOnHand()).c_str()) << endl << "The price of the book is: $" << inventory[index].getpriceBook() << endl << "The total selling price is: $" << atof((inventory[index].getpriceBook()).c_str())* atoi((inventory[index].getnumberOnHand()).c_str()) << endl << "The name of the first author is: " << inventory[index].getbookAuthors(0) << endl << "The name of the second author is: " << inventory[index].getbookAuthors(1) << endl << "The name of the third author is: " << inventory[index].getbookAuthors(2) << endl << "The name of the fourth author? " << inventory[index].getbookAuthors(3) << endl << endl; cout << "Continue? "; char resp; cin.get(resp).get(); } } // This function permits the editing of the individual record items // one at a time given an the ISBN of the book. // void editItems(bookInventory inventory[],int const &numberItems) { string aString; char resp; int recordNumber = -1; // The ISBN for the record to be edited is requested. // clearScreen(); cout << "What is the ISBN? "; getline(cin,aString); for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == aString) { recordNumber = index; break; } } // If no book exists in the inventory array with the // requested ISBN, an error message is returned. // if(recordNumber == -1) { clearScreen(); cout << "Error!! There is no such book on record. Continue? "; cin.get(resp).get(); } else { // The current data for the book with the requested // ISBN is displayed. // clearScreen(); cout << "The current data on that book is:" << endl << endl << "The book name is: " << inventory[index].getbookName() << endl << "The ISBN is: " << inventory[index].getISBN() << endl << "The year the book was published: " << inventory[index].getyearPublished() << endl << "The number of this book that we have on hand is: " << inventory[index].getnumberOnHand() << endl << "The cost of the book was: $" << inventory[index].getcostBook() << endl << "The price of the book is: $" << inventory[index].getpriceBook() << endl << "The name of the first author is: " << inventory[index].getbookAuthors(0) << endl << "The name of the second author is: " << inventory[index].getbookAuthors(1) << endl << "The name of the third author is: " << inventory[index].getbookAuthors(2) << endl << "The name of the fourth author? " << inventory[index].getbookAuthors(3) << endl << endl; cout << "Continue? "; cin.get(resp).get(); // The new data for the record is requested. // cout << endl << endl << "What is the book name? "; getline(cin,aString); inventory[index].setbookName(aString); cout << endl << "What is the ISBN? "; getline(cin,aString); inventory[index].setISBN(aString); cout << endl << "What is the year the book was published? "; getline(cin,aString); inventory[index].setyearPublished(aString); cout << endl << "How many of this book do we have on hand? "; getline(cin,aString); inventory[index].setnumberOnHand(aString); cout << endl << "What was the cost of the book? "; getline(cin,aString); inventory[index].setcostBook(aString); cout << endl << "What is the price of the book? "; getline(cin,aString); inventory[index].setpriceBook(aString); cout << endl << "What is the name of the first author? "; getline(cin,aString); inventory[index].setbookAuthors(0,aString); cout << endl << "What is the name of the second author? "; getline(cin,aString); inventory[index].setbookAuthors(1,aString); cout << endl << "What is the name of the third author? "; getline(cin,aString); inventory[index].setbookAuthors(2,aString); cout << endl << "What is the name of the fourth author? "; getline(cin,aString); inventory[index].setbookAuthors(3,aString); cout << endl << "Continue? "; cin.get(resp).get(); } } // This function permits the deleting from a record that has been // selected by using the ISBN. It first asks for the ISBN and then // if the user desires it to be deleted the record is filed with // empty data. // void deleteItem(bookInventory inventory[],int const &numberItems) { string aString; char resp; int recordNumber = -1; clearScreen(); // The ISBN for the book to be deleted is requested. // cout << "What is the ISBN of the book you want to delete? "; getline(cin,aString); // The records of the inventory array are searched to // find the record with the requested ISBN. // for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == aString) { recordNumber = index; break; } } // If no record exists with the requested ISBN, // an error message is returned. // if(recordNumber == -1) { clearScreen(); cout << "Error!! There is no such book on record. Continue? "; cin.get(resp).get(); } else { clearScreen(); // If the record exists, the data is displayed in order // for the user to check to ensure that the record found // is the record to be deleted. // cout << "The current data on the book to be deleted is:" << endl << endl << "The book name is: " << inventory[index].getbookName() << endl << "The ISBN is: " << inventory[index].getISBN() << endl << "The year the book was published: " << inventory[index].getyearPublished() << endl << "The number of this book that we have on hand is: " << inventory[index].getnumberOnHand() << endl << "The cost of the book was: $" << inventory[index].getcostBook() << endl << "The price of the book is: $" << inventory[index].getpriceBook() << endl << "The name of the first author is: " << inventory[index].getbookAuthors(0) << endl << "The name of the second author is: " << inventory[index].getbookAuthors(1) << endl << "The name of the third author is: " << inventory[index].getbookAuthors(2) << endl << "The name of the fourth author? " << inventory[index].getbookAuthors(3) << endl << endl; cout << "Continue to delete? (Y/N)"; cin.get(resp).get(); // The user is requested to confirm that the displayed data // is the record to be deleted. If it is, then the // record is filled with empty data. // if((resp=='Y') | (resp == 'y')) { string startString = "Start"; string aString = "****"; string endString = "End"; inventory[index].setbookName(startString); inventory[index].setISBN(aString); inventory[index].setyearPublished(aString); inventory[index].setnumberOnHand(aString); inventory[index].setcostBook(aString); inventory[index].setpriceBook(aString); inventory[index].setbookAuthors(0,aString); inventory[index].setbookAuthors(1,aString); inventory[index].setbookAuthors(2,aString); inventory[index].setbookAuthors(3,endString); } } } // This function stores the data that is contained in the // inventory array into the file for later use. This is // called prior to the program ending. // void saveInventory(bookInventory inventory[],int const &numberItems) { // The data file is opened for outputing the records // that are stored in the inventory array prior to // ending the program. // string aString; ofstream outFile("inventory.dat"); // Each record in the inventory array is sent to // the data file. // for(int index = 0; index < numberItems; ++ index) { aString = inventory[index].getbookName(); outFile << aString << endl; aString = inventory[index].getISBN(); outFile << aString << endl; aString = inventory[index].getyearPublished(); outFile << aString << endl; aString = inventory[index].getnumberOnHand(); outFile << aString << endl; aString = inventory[index].getcostBook(); outFile << aString << endl; aString = inventory[index].getpriceBook(); outFile << aString << endl; aString = inventory[index].getbookAuthors(0); outFile << aString << endl; aString = inventory[index].getbookAuthors(1); outFile << aString << endl; aString = inventory[index].getbookAuthors(2); outFile << aString << endl; aString = inventory[index].getbookAuthors(3); outFile << aString << endl; } outFile.close(); } // program-id theNewFunctions.h // written-by Don Voils // date written 5/27/2006 // // Description This file contains the definition of the // function that are the modules for the // program doInventory.cpp. This is a modification // of the header file: theFunctions.h // #include<iostream> #include<string> #include<fstream> #include<iomanip> #include<cstdlib> using namespace std; // The defintion of the bookInventory class // must be included into the program. // #include"bookInventory.h" // This function is used to clear the screen for // a better user presentation. // void clearScreen() { for(int loop_counter=0;loop_counter<=250;++loop_counter) cout << endl; } // This function is used to load from file the book records // and then stores them into the inventory array. // void loadBinaryInventory(bookInventory inventory[],int const &numberItems) { // The data file that holds the inventory is opened // ifstream inFile("binaryinventory.dat",ios::binary|ios::in); // If the file is empty, then it is filled with // empty data. // if(!inFile) { string startString = "Start"; string aString = "****"; //cout << "Starting." << endl; //char resp; //cin.get(resp).get(); for(int index = 0; index < numberItems;++index) { inventory[index].setbookName(startString); inventory[index].setISBN(aString); inventory[index].setyearPublished(aString); inventory[index].setnumberOnHand(aString); inventory[index].setcostBook(aString); inventory[index].setpriceBook(aString); inventory[index].setbookAuthors(0,aString); inventory[index].setbookAuthors(1,aString); inventory[index].setbookAuthors(2,aString); inventory[index].setbookAuthors(3,"end"); } } else { // If the file is not empty, it is input into // the array of inventory items array. // string aString; for(int index = 0;index<numberItems;++index) { inFile.read((char*)&inventory[index],sizeof(inventory[index])); } } inFile.close(); } // This function permits the entry of a book records. It first // finds an empty record in the inventory array and then permits // the individual data items to be entered into the particular record. // void enterNewRecords(bookInventory inventory[],int const &numberItems) { // The inventory array is searched for an empty record. // int recordNumber = -1; for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == "****") { recordNumber = index; break; } } // If the inventory array has no more empty records, the // user is notified of that case. // if(recordNumber == -1) { clearScreen(); cout << "Error file is full. Continue? " << endl; char resp; cin.get(resp).get(); } else { // If the inventory array has an empty record, // the data is entered into that record. // clearScreen(); string aString; cout << "What is the book name? "; getline(cin,aString); inventory[index].setbookName(aString); cout << endl << "What is the ISBN? "; getline(cin,aString); inventory[index].setISBN(aString); cout << endl << "What is the year the book was published? "; getline(cin,aString); inventory[index].setyearPublished(aString); cout << endl << "How many of this book do we have on hand? "; getline(cin,aString); inventory[index].setnumberOnHand(aString); cout << endl << "What was the cost of the book? "; getline(cin,aString); inventory[index].setcostBook(aString); cout << endl << "What is the price of the book? "; getline(cin,aString); inventory[index].setpriceBook(aString); cout << endl << "What is the name of the first author? "; getline(cin,aString); inventory[index].setbookAuthors(0,aString); cout << endl << "What is the name of the second author? "; getline(cin,aString); inventory[index].setbookAuthors(1,aString); cout << endl << "What is the name of the third author? "; getline(cin,aString); inventory[index].setbookAuthors(2,aString); cout << endl << "What is the name of the fourth author? "; getline(cin,aString); inventory[index].setbookAuthors(3,aString); } } // This function displays the data values of a particular record // given the ISBN of the book. // void ISBNDisplay(bookInventory inventory[],int const &numberItems) { string aString; clearScreen(); // The ISBN of the record to be displayed is requested. // cout << "What is the ISBN? "; getline(cin,aString); // The manipulators for output is set up. // cout << setprecision(2); cout << fixed; // The inventory array is searched to determine // whether a record exists with the requested // ISBN. // int recordNumber = -1; for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == aString) { recordNumber = index; break; } } // If no record exists for the requested ISBN, // an error message is returned. // if(recordNumber == -1) { clearScreen(); cout << "Error. There is no book with that ISBN. Continue? "; char resp; cin.get(resp).get(); } else { // When the record for the requested ISBN book exists, // the data for the book is displayed. // clearScreen(); cout << "The book name is: " << inventory[index].getbookName() << endl << "The ISBN is: " << inventory[index].getISBN() << endl << "The year the book was published: " << inventory[index].getyearPublished() << endl << "The number of this book that we have on hand is: " << inventory[index].getnumberOnHand() << endl << "The cost of the book was $: " << inventory[index].getcostBook() << endl << "The total cost is: $" << atof((inventory[index].getcostBook()).c_str())* atoi((inventory[index].getnumberOnHand()).c_str()) << endl << "The price of the book is: $" << inventory[index].getpriceBook() << endl << "The total selling price is: $" << atof((inventory[index].getpriceBook()).c_str())* atoi((inventory[index].getnumberOnHand()).c_str()) << endl << "The name of the first author is: " << inventory[index].getbookAuthors(0) << endl << "The name of the second author is: " << inventory[index].getbookAuthors(1) << endl << "The name of the third author is: " << inventory[index].getbookAuthors(2) << endl << "The name of the fourth author? " << inventory[index].getbookAuthors(3) << endl << endl; cout << "Continue? "; char resp; cin.get(resp).get(); } } // This function permits the editing of the individual record items // one at a time given an the ISBN of the book. // void editItems(bookInventory inventory[],int const &numberItems) { string aString; char resp; int recordNumber = -1; // The ISBN for the record to be edited is requested. // clearScreen(); cout << "What is the ISBN? "; getline(cin,aString); for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == aString) { recordNumber = index; break; } } // If no book exists in the inventory array with the // requested ISBN, an error message is returned. // if(recordNumber == -1) { clearScreen(); cout << "Error!! There is no such book on record. Continue? "; cin.get(resp).get(); } else { // The current data for the book with the requested // ISBN is displayed. // clearScreen(); cout << "The current data on that book is:" << endl << endl << "The book name is: " << inventory[index].getbookName() << endl << "The ISBN is: " << inventory[index].getISBN() << endl << "The year the book was published: " << inventory[index].getyearPublished() << endl << "The number of this book that we have on hand is: " << inventory[index].getnumberOnHand() << endl << "The cost of the book was: $" << inventory[index].getcostBook() << endl << "The price of the book is: $" << inventory[index].getpriceBook() << endl << "The name of the first author is: " << inventory[index].getbookAuthors(0) << endl << "The name of the second author is: " << inventory[index].getbookAuthors(1) << endl << "The name of the third author is: " << inventory[index].getbookAuthors(2) << endl << "The name of the fourth author? " << inventory[index].getbookAuthors(3) << endl << endl; cout << "Continue? "; cin.get(resp).get(); // The new data for the record is requested. // cout << endl << endl << "What is the book name? "; getline(cin,aString); inventory[index].setbookName(aString); cout << endl << "What is the ISBN? "; getline(cin,aString); inventory[index].setISBN(aString); cout << endl << "What is the year the book was published? "; getline(cin,aString); inventory[index].setyearPublished(aString); cout << endl << "How many of this book do we have on hand? "; getline(cin,aString); inventory[index].setnumberOnHand(aString); cout << endl << "What was the cost of the book? "; getline(cin,aString); inventory[index].setcostBook(aString); cout << endl << "What is the price of the book? "; getline(cin,aString); inventory[index].setpriceBook(aString); cout << endl << "What is the name of the first author? "; getline(cin,aString); inventory[index].setbookAuthors(0,aString); cout << endl << "What is the name of the second author? "; getline(cin,aString); inventory[index].setbookAuthors(1,aString); cout << endl << "What is the name of the third author? "; getline(cin,aString); inventory[index].setbookAuthors(2,aString); cout << endl << "What is the name of the fourth author? "; getline(cin,aString); inventory[index].setbookAuthors(3,aString); cout << endl << "Continue? "; cin.get(resp).get(); } } // This function permits the deleting from a record that has been // selected by using the ISBN. It first asks for the ISBN and then // if the user desires it to be deleted the record is filed with // empty data. // void deleteItem(bookInventory inventory[],int const &numberItems) { string aString; char resp; int recordNumber = -1; clearScreen(); // The ISBN for the book to be deleted is requested. // cout << "What is the ISBN of the book you want to delete? "; getline(cin,aString); // The records of the inventory array are searched to // find the record with the requested ISBN. // for(int index = 0; index < numberItems;++index) { if(inventory[index].getISBN() == aString) { recordNumber = index; break; } } // If no record exists with the requested ISBN, // an error message is returned. // if(recordNumber == -1) { clearScreen(); cout << "Error!! There is no such book on record. Continue? "; cin.get(resp).get(); } else { clearScreen(); // If the record exists, the data is displayed in order // for the user to check to ensure that the record found // is the record to be deleted. // cout << "The current data on the book to be deleted is:" << endl << endl << "The book name is: " << inventory[index].getbookName() << endl << "The ISBN is: " << inventory[index].getISBN() << endl << "The year the book was published: " << inventory[index].getyearPublished() << endl << "The number of this book that we have on hand is: " << inventory[index].getnumberOnHand() << endl << "The cost of the book was: $" << inventory[index].getcostBook() << endl << "The price of the book is: $" << inventory[index].getpriceBook() << endl << "The name of the first author is: " << inventory[index].getbookAuthors(0) << endl << "The name of the second author is: " << inventory[index].getbookAuthors(1) << endl << "The name of the third author is: " << inventory[index].getbookAuthors(2) << endl << "The name of the fourth author? " << inventory[index].getbookAuthors(3) << endl << endl; cout << "Continue to delete? (Y/N)"; cin.get(resp).get(); // The user is requested to confirm that the displayed data // is the record to be deleted. If it is, then the // record is filled with empty data. // if((resp=='Y') | (resp == 'y')) { string startString = "Start"; string aString = "****"; string endString = "End"; inventory[index].setbookName(startString); inventory[index].setISBN(aString); inventory[index].setyearPublished(aString); inventory[index].setnumberOnHand(aString); inventory[index].setcostBook(aString); inventory[index].setpriceBook(aString); inventory[index].setbookAuthors(0,aString); inventory[index].setbookAuthors(1,aString); inventory[index].setbookAuthors(2,aString); inventory[index].setbookAuthors(3,endString); } } } // This function stores the data that is contained in the // inventory array into the file for later use. This is // called prior to the program ending. This is a modification of // the function saveInventory(). // void saveBinaryInventory(bookInventory inventory[],int const &numberItems) { // The data file is opened for outputing the records // that are stored in the inventory array prior to // ending the program. // string aString; ofstream outFile("binaryinventory.dat",ios::out|ios::binary); // Each record in the inventory array is sent to // the data file. // for(int index = 0; index < numberItems; ++ index) { outFile.write((char*)&inventory[index],sizeof(inventory[index])); } outFile.close(); } // program_id truncate.cpp // author don voils // date written 3/25/2006 // // description This program demonstrates output // by truncating the data in a diskfile. // #include<fstream> #include<iostream> using namespace std; void main() { float theHourlyRate = 125.50; int hoursWorked = 45; ofstream outfile("testin2.txt",ios::ate); if(!outfile) { cout << "File did not open." << endl << endl; exit(1); } outfile << "George" << endl; outfile << "Thomas" << endl; outfile << "Accountant" << endl; outfile << theHourlyRate << endl; outfile << hoursWorked << endl; }

Категории