Objectives
Academic HonestyRead the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:
Grading Criteria (30pts + Extra Credit 5 pts)For each part: 15 pts: File Header 1pt, Function Headers 1pt, Compiles 2pts, Proper Formatting 1pt, Works as Specified 10pts
Part 2: You must put in the comment area a note that you used your own contacts file to start with for the extra 2 points. Total Possible: 30 pts + Extra Credit 5 pts Project Specifications Your solutions to these projects must use only techniques we have covered so far. Programming StyleFor all programs, remember to follow all the style rules we have covered, as well as the newer rules, including:
Project 1: File WorksheetFiles are a way to store data that is retained even after a program ends or a computer is turned off. To save data we write to a file. To recover the data, we read from the file.Use this worksheet to improve your understanding of files, vectors and functions. Refer to lesson 11 and lesson 12 for more information on files. Project Specifications
Project 2: Contacts FileWe continue to develop out contact list manager application. We load our data from a file and save our vector of When developing this program, you will use your Contact List code from Assignment 10. You will add new files to store data for our application. (Image by Hopstarter) Project Specifications
Extra Credit (5 pts)The following are worth extra credit points:
SOLUTION CODE: filework.cpp /** filework.cpp Purpose: a worksheet to improve your skills with files, functions, objects and vectors. @author Sharon Strelitz @version 1.0 12/25/2018 */ #include <cstdlib> // for exit() #include <fstream> // for file I/O #include <iostream> // for cout #include <vector> // for vectors #include "helper.cpp" // worksheet helper, see 12.4.1 using namespace std; /** Read a file of integers and returns the data via reference parameters. @param myInts The vector of integers to read into. @param fileName The name of the file to read from. @see: Lesson 12.1.2, Exercise 12.1 */ void readInts(vector<int>& myInts, string fileName); /** Read a file of strings and returns the data via reference parameters. @param myStrs The vector of integers to read into. @param fileName The name of the file to read from. @see: Lesson 12.1.2, 12.1.4 */ void readStrs(vector<string>& myStrs, string fileName); /** Read a file of strings with spaces and returns the data via reference parameters. @param myNames The vector of integers to read into. @param fileName The name of the file to read from. @see: Lesson 12.1.3 */ void readNames(vector<string>& myNames, string fileName); /** Opens the file and calls Product::read(ifstream&) in a while-loop to read all the product names and prices from the file, storing the data in the single vector<product> myProds. @param myProds The vector of integers to read into. @param fileName The name of the file to read from. @see: Lesson 12.3.5, Exercises 12.2, 12.3 */ void readProds(vector<Product>& myProds, string fileName); int main() { vector<int> myInts; vector<string> myStrs; vector<string> names; vector<Product> prods; setup(); // create files: ints.txt, strs.txt, names.txt, prods.txt /* Remove comments as you develop functions.*/ cout << "\n***Testing readInts***"<< endl; readInts(myInts, "ints.txt"); cout << "readInts vector size should be 3: " << myInts.size() << endl; cout << "readInts0 should be 27: " << myInts[0] << endl; cout << "readInts1 should be 39: " << myInts[1] << endl; cout << "readInts2 should be 42: " << myInts[2] << endl; /* Remove comments as you develop functions. */ cout << "\n***Testing readStrs***"<< endl; readStrs(myStrs, "strs.txt"); cout << "readStrs vector size should be 3: " << myStrs.size() << endl; cout << "readStrs0 should be C++: " << myStrs[0] << endl; cout << "readStrs1 should be still: " << myStrs[1] << endl; cout << "readStrs2 should be rules: " << myStrs[2] << endl; /* Remove comments as you develop functions.*/ cout << "\n***Testing readNames***"<< endl; readNames(names, "names.txt"); cout << "readNames vector size should be 4: " << names.size() << endl; cout << "readNames0 should be Sophie Engineer: " << names[0] << endl; cout << "readNames1 should be Emma Hacker: " << names[1] << endl; cout << "readNames2 should be John Q Public: " << names[2] << endl; const int IDX3 = 3; cout << "readNames3 should be Joe Schmoe: " << names[IDX3] << endl; /* Remove comments as you develop functions. */ cout << "\n***Testing readProds***"<< endl; readProds(prods, "products.txt"); cout << "readProds vector size should be 3: " << prods.size() << endl; cout << "readProds0 should be Milk @ 3.95: "; prods[0].print(); cout << "readProds1 should be Fresh bread @ 2.99: "; prods[1].print(); cout << "readProds2 should be Cheddar cheese @ 4.99: "; prods[2].print(); cout << "\n***End of Tests***" << endl; return 0; } void readInts(vector<int>& myInts, string fileName) { ifstream fin(fileName); if (fin.fail()) { cout << "Input file failed to open.\n"; exit(-1); } int input; while (fin >> input) { myInts.push_back(input); } } void readStrs(vector<string>& myStrs, string fileName) { ifstream fin(fileName); if (fin.fail()) { cout << "Input file failed to open.\n"; exit(-1); } string input; while (fin >> input) { myStrs.push_back(input); } } void readNames(vector<string>& myNames, string fileName) { ifstream fin(fileName); if (fin.fail()) { cout << "Input file failed to open.\n"; exit(-1); } string input; while (getline(fin, input)) { myNames.push_back(input); } } void readProds(vector<Product>& myProds, string fileName) { ifstream fin(fileName); if (fin.fail()) { cout << "Input file failed to open.\n"; exit(-1); } Product tempProd; while (fin.good()) { tempProd.read(fin); if (fin.good()) { // verify not end-of-file myProds.push_back(tempProd); } } } contactfile.cpp /** CS-11 Asn 12, contactfile.cpp Purpose: keeping track of people and uses files to store and load the people @author Sharon Strelitz @version 1.0 10/22/2017 */ #include <vector> #include <iostream> #include <iomanip> #include <sstream> // for extra credit #include <fstream> using namespace std; class Person { public: /** Default constructor assigns default values. */ Person(); /** Constructs a Person object. @param name Name of the person. @param income Income to the Earth in miles. @param age Number of new age for this person. */ Person(string name, int age, double income); /** Returns the name of this person. @return The name of this person. */ string getName() const; /** Returns the age of this person. @return The age of this person. */ int getAge() const; /** Returns the income of this person. @return The income of this person in miles. */ double getIncome() const; /** Changes the name of this person. @param newName New name of this person. */ void setName(string newName); /** Changes the age of this person. @param newAge New age of this person. */ void setAge(int newAge); /** Changes the income of this person. @param newIncome New income of this person. */ void setIncome(double newIncome); /** Displays information about this person to the screen. */ void print() const; /** Reads person data from the keyboard. */ void read(); /** Loads information about this Person from the file stream. @param fin A file input stream connected to the files with the data to load. */ void read(ifstream& fin); /** Writes information about this Person to the file stream. @param fout A file output stream connected to the file in which to save the data. */ void write(ofstream& fout) const; private: string name; // name of person int age; // age of person double income; // income of person }; // Formatting constants const int NAME_WIDTH = 18; const int AGE_WIDTH = 6; const int INCOME_WIDTH = 11; // Default constructor Person::Person() { age = 0; income = 0.0; } // Overloaded constructor Person::Person(string name, int age, double income) { setName(name); setAge(age); setIncome(income); } // Accessor functions string Person::getName() const { return name; } int Person::getAge() const { return age; } double Person::getIncome() const { return income; } void Person::print() const { cout << setw(NAME_WIDTH) << left << name << right << setw(AGE_WIDTH) << age << fixed << setprecision(2) << setw(INCOME_WIDTH) << income << endl; } // Mutator functions void Person::setName(string newName) { name = newName; } void Person::setAge(int newAge) { if (newAge >= 0) { age = newAge; } else { age = 0; cout << "Age cannot be negative; setting to zero.\n"; } } void Person::setIncome(double newIncome) { income = newIncome; } void Person::read() { cout << "Enter the name of the person: "; cin >> ws; getline(cin, name); // Making use of the extra credit function cout << "Enter the age for " << name << ": "; cin >> age; cout << "Enter the income for " << name << ": "; cin >> income; } void Person::read(ifstream& fin) { fin >> ws; getline(fin, name); fin >> age; fin >> income; } void Person::write(ofstream& fout) const { fout << name << endl; fout << age << endl; fout << fixed << setprecision(2); fout << income << endl; } /** Lists the people on the list. @param list The list of Person objects. */ void listContacts(vector<Person>& list); /** Adds a person to the list. @param list The list of Person objects. */ void addContact(vector<Person>& list); /** Deletes a person from the list. @param list The list of Person objects. */ void deleteContact(vector<Person>& list); /** Changes the income for a contact. @param list The list of Person objects. */ void changeIncome(vector<Person>& list); /** List contacts within an age bracket. @param list The list of Person objects. */ void listByAge(vector<Person>& list); /** Loads all contact data from the specified file name and returns the data in a vector of Person objects. @param list The list of contacts read from the file. @param fileName The name of the file from which to read. */ void loadData(vector<Person>& list, string fileName); /** Writes contact data to an output file. @param list The vector of Person objects. @param fileName The name of the file to which to write. */ void saveData(const vector<Person>& list, string fileName); int main() { // Menu items const int LIST = 1; const int ADD = 2; const int DEL = 3; const int CHG = 4; const int AGE = 5; const int EXIT = 0; // Other constants /* const int AGE1 = 42; const int AGE2 = 24; const int AGE3 = 37; const double MEDIAN_SOFT_DEV = 102280.0; const double AVG_CS_NEWGRAD = 71916.0; const double AVG_US = 55775.37; Person sophie; sophie.setName("Sophie Engineer"); sophie.setAge(AGE1); sophie.setIncome(MEDIAN_SOFT_DEV); Person emma("Emma Hacker", AGE2, AVG_CS_NEWGRAD); Person john("John Q Public", AGE3, AVG_US); vector<Person> list; list.push_back(sophie); list.push_back(emma); list.push_back(john); */ vector<Person> list; loadData(list,"contacts.txt"); cout << "Welcome to the Contact List Manager\n"; int choice = 1; while (choice != 0) { string msg = "\nPlease choose one of the following operations:\n"; msg += "0. Exit program\n"; msg += "1. List contacts\n"; msg += "2. Add a contact\n"; msg += "3. Delete a contact\n"; msg += "4. Change contact income\n"; msg += "5. List by age\n"; msg += "Choice (0-5): "; cout << msg; cin >> choice; if (choice == LIST) { listContacts(list); } else if (choice == ADD) { addContact(list); } else if (choice == DEL) { deleteContact(list); } else if (choice == CHG) { changeIncome(list); } else if (choice == AGE) { listByAge(list); } else if (choice != EXIT) { cout << "\nInvalid choice!\n"; } } saveData(list, "contacts.txt"); cout << "\nGoodbye!\n"; return 0; } void listContacts(vector<Person>& list) { const int HASH_WIDTH = 3; cout << endl; cout << "Contact list:\n"; cout << setw(NAME_WIDTH + HASH_WIDTH) << left << " # Name" << setw(AGE_WIDTH) << right << "Age" << setw(INCOME_WIDTH) << right << "Income" << endl; for (unsigned num = 0; num < list.size(); num++) { cout << setw(2) << right << (num + 1) << " "; list[num].print(); } } // Add person to list by calling read() void addContact(vector<Person>& list) { cout << "\nAdding a new contact:\n"; Person peep; peep.read(); list.push_back(peep); } void deleteContact(vector<Person>& list) { cout << "\nDeleting a contact:"; listContacts(list); cout << "Enter the number of the contact: "; int pos; cin >> pos; pos--; // adjust for vector starting at 0 for (unsigned i = pos; i < list.size() - 1; i++) { list[i] = list[i + 1]; } list.pop_back(); } void changeIncome(vector<Person>& list) { cout << "\nChange income for a contact:"; listContacts(list); cout << "Enter the number of the contact: "; int pos; cin >> pos; pos--; // adjust for vector starting at 0 double income = list[pos].getIncome(); cout << "Enter the new income: "; cin >> income; list[pos].setIncome(income); } void listByAge(vector<Person>& list) { int min = 0; int max = list.size() - 1; cout << "\nSearching by Age\n"; cout << "Enter the minimum age: "; cin >> min; cout << "Enter the maximum age: "; cin >> max; cout << setw(NAME_WIDTH) << left << "Name" << setw(AGE_WIDTH) << right << "Age" << setw(INCOME_WIDTH) << right << "Income" << endl; for (unsigned i = 0; i < list.size(); i++) { int age = list[i].getAge(); if (age >= min and age <= max) { list[i].print(); } } } void loadData(vector<Person>& list, string fileName) { ifstream fin(fileName.c_str()); if (fin.fail()) { cout << "error opening input file" << endl; exit(-1); } while(fin.good()) { Person temp; temp.read(fin); if (fin.good()) { list.push_back(temp); } } fin.close(); } void saveData(const vector<Person>& list, string fileName) { ofstream fout(fileName.c_str()); if (fout.fail()) { cout << "error opening output file" << endl; exit(-1); } fout << fixed << setprecision(2); // two decimal places for (int i = 0; i < (int) list.size(); i++) { list[i].write(fout); } fout.close(); } |
Schedule >