Image

Imagelilbrattyteen wrote in Imagecpp

This is a program that maintains a vector of record files. It can read/write the records to/from a disk file and delete records.

I put the read/write functions in the other day and all of a sudden it won't work. It builds properly, but when I add records then try to display them, it says I don't have any records inputted.




1) /* 2) comment the code! 3) perfect the remove-record functions 4) idiotproof input 5) */ 6) 7) #include <iostream> 8) #include <string> 9) #include <vector> 10) #include <fstream> 11) using namespace std; 12) 13) class InventoryRecord //Defines record class. 14) { 15) string Desc; 16) string ID; 17) float Cost; 18) string Location; 19) int Total; 20) 21) public: 22) InventoryRecord() {;} //Record class constructor. 23) 24) void SetDesc (string DescTemp) { Desc = DescTemp; } 25) void SetID (string IDTemp) { ID = IDTemp; } 26) void SetCost (string CostTemp) { Cost = atof(CostTemp.c_str()); } 27) void SetLocation (string LocationTemp) { Location = LocationTemp; } 28) void SetTotal (string TotalTemp) { Total = atoi(TotalTemp.c_str()); } 29) 30) string GetDesc() { return Desc; } 31) string GetID() { return ID; } 32) float GetCost() { return Cost; } 33) string GetLocation() { return Location; } 34) int GetTotal() { return Total; } 35) 36) void DisplayRecord(); 37) }; 38) 39) void InventoryRecord::DisplayRecord() //Displays the contents of a record. 40) { 41) cout << "\n==================================================="; 42) cout << "\nThis is the entire record. "; 43) cout << "\nItem ID: " << GetID(); 44) cout << "\nItem description: " << GetDesc(); 45) cout << "\nItem cost: " << GetCost(); 46) cout << "\nItem location: " << GetLocation(); 47) cout << "\nItem quantity: " << GetTotal(); 48) cout << "\n==================================================="; 49) } 50) 51) 52) class InventoryRecordVector //This class is a vector of record classes. The vector manages the list of records. 53) { 54) vector<InventoryRecord> RecordList; 55) 56) string DescTemp; 57) string IDTemp; 58) string CostTemp; 59) string LocationTemp; 60) string TotalTemp; 61) 62) public: 63) InventoryRecordVector() {;} 64) vector<InventoryRecord>::iterator RecordListIterator; //Defines the vector's iterator. 65) 66) void AddRecords(); 67) void FindRecord(); 68) void RemoveRecordsByID(); 69) void RemoveRecordsByDescription(); 70) void RemoveRecordsByCost(); 71) void RemoveRecordsByLocation(); 72) void RemoveRecordsByTotal(); 73) void ShowRecords(); 74) void OutputListToFile(); 75) void LoadListFromFile(); 76) void NewList(); 77) }; 78) 79) void InventoryRecordVector::AddRecords() //Adds record objects to the end of the vector. 80) { 81) InventoryRecord RecordName; 82) 83) cout << "\nEnter records below. Type 'stop' to quit." << endl; 84) 85) for(;;) 86) { 87) cout << "Item ID: "; 88) getline(cin, IDTemp); 89) if(IDTemp == "Stop" || IDTemp == "stop") 90) break; 91) cout << "Item Description: "; 92) getline(cin, DescTemp); 93) if(DescTemp == "Stop" || DescTemp == "stop") 94) break; 95) cout << "Item Cost: "; 96) getline(cin, CostTemp); 97) if(CostTemp == "Stop" || CostTemp == "stop") 98) break; 99) cout << "Item Quantity: "; 100) getline(cin, TotalTemp); 101) if(TotalTemp == "Stop" || TotalTemp == "stop") 102) break; 103) cout << "Item Location: "; 104) getline(cin, LocationTemp); 105) if(LocationTemp == "Stop" || LocationTemp == "stop") 106) break; 107) 108) RecordName.SetDesc(DescTemp); 109) RecordName.SetID(IDTemp); 110) RecordName.SetCost(CostTemp); 111) RecordName.SetTotal(TotalTemp); 112) RecordName.SetLocation(LocationTemp); 113) 114) RecordList.push_back(RecordName); 115) } 116) } 117) 118) void InventoryRecordVector::FindRecord() //This will search for and display a record on the list.. 119) { 120) string SearchFor; 121) int SearchResults; 122) 123) cout << "\nThis search function will work with any of the record fields: ID, cost, description, quantity, and location."; 124) cout << "\nEnter Search Value: "; 125) getline(cin, SearchFor); 126) 127) for(RecordListIterator = RecordList.begin() ; RecordListIterator != RecordList.end() ; RecordListIterator++) 128) { 129) if(RecordListIterator->GetDesc() == SearchFor) { 130) RecordListIterator->DisplayRecord(); 131) SearchResults++; } 132) if(RecordListIterator->GetID() == SearchFor) { 133) RecordListIterator->DisplayRecord(); 134) SearchResults++; } 135) if(RecordListIterator->GetCost() == atof(SearchFor.c_str())) { 136) RecordListIterator->DisplayRecord(); 137) SearchResults++; } 138) if(RecordListIterator->GetLocation() == SearchFor) { 139) RecordListIterator->DisplayRecord(); 140) SearchResults++; } 141) if(RecordListIterator->GetTotal() == atoi(SearchFor.c_str())) { 142) RecordListIterator->DisplayRecord(); 143) SearchResults++; } 144) } 145) 146) if (SearchResults == 0) 147) cout << SearchFor << " Not Found" << endl; 148) } 149) 150) void InventoryRecordVector::RemoveRecordsByID() //This function searches for records by the ID field and deletes them. 151) { 152) string SearchFor; 153) int SearchResults, ToDeleteOrNotToDelete; 154) 155) cin.ignore(10, '\n'); 156) cout << "\nEnter Search Value: "; 157) getline(cin, SearchFor); 158) 159) for (RecordListIterator = RecordList.begin() ; ; RecordListIterator++) 160) { 161) if(RecordListIterator->GetID() == SearchFor) 162) { 163) RecordListIterator->DisplayRecord(); 164) SearchResults++; 165) cout << "\nWould you like to delete this record? Enter 1 if yes and 0 if no: "; 166) cin >> ToDeleteOrNotToDelete; 167) if (ToDeleteOrNotToDelete == 1) 168) { 169) RecordList.erase(RecordListIterator); 170) cout << "\nRecord erased." << endl; 171) } 172) if (ToDeleteOrNotToDelete == 0) 173) cout << "\nRecord saved." << endl; 174) else if ( (ToDeleteOrNotToDelete != 1) && (ToDeleteOrNotToDelete == 0) ) 175) cout << "Invalid input." << endl; 176) } 177) 178) if (RecordListIterator == RecordList.end()) 179) { 180) cout << "\nSearch done." << endl; 181) break; 182) } 183) } 184) 185) if (SearchResults == 0) 186) cout << SearchFor << "\nNo results found." << endl; 187) } 188) 189) 190) void InventoryRecordVector::RemoveRecordsByDescription() //This function searches for records by the description field and deletes them. 191) { 192) string SearchFor; 193) int SearchResults, ToDeleteOrNotToDelete; 194) 195) cin.ignore(10, '\n'); 196) cout << "\nEnter Search Value: "; 197) getline(cin, SearchFor); 198) 199) for (RecordListIterator = RecordList.begin() ; RecordListIterator != RecordList.end() ; RecordListIterator++) 200) { 201) if(RecordListIterator->GetDesc() == SearchFor) 202) { 203) RecordListIterator->DisplayRecord(); 204) SearchResults++; 205) cout << "\nWould you like to delete this record? Enter 1 if yes and 0 if no: "; 206) cin >> ToDeleteOrNotToDelete; 207) if (ToDeleteOrNotToDelete == 1) { 208) RecordList.erase(RecordListIterator); 209) cout << "\nRecord erased." << endl; } 210) if (ToDeleteOrNotToDelete == 0) { 211) cout << "\nRecord saved." << endl; 212) } 213) else if ( (ToDeleteOrNotToDelete != 1) && (ToDeleteOrNotToDelete == 0) ) 214) cout << "Invalid input." << endl; 215) } 216) } 217) 218) if (SearchResults == 0) 219) cout << SearchFor << " Not Found" << endl; 220) } 221) 222) void InventoryRecordVector::RemoveRecordsByCost() //This function searches for records by the cost field and deletes them. 223) { 224) float SearchFor; 225) int SearchResults, ToDeleteOrNotToDelete; 226) 227) cin.ignore(10, '\n'); 228) cout << "\nEnter Search Value: "; 229) cin >> SearchFor; 230) 231) for (RecordListIterator = RecordList.begin() ; RecordListIterator != RecordList.end() ; RecordListIterator++) 232) { 233) if(RecordListIterator->GetCost() == SearchFor) 234) { 235) RecordListIterator->DisplayRecord(); 236) SearchResults++; 237) cout << "\nWould you like to delete this record? Enter 1 if yes and 0 if no: "; 238) cin >> ToDeleteOrNotToDelete; 239) if (ToDeleteOrNotToDelete == 1) { 240) RecordList.erase(RecordListIterator); 241) cout << "\nRecord erased." << endl; } 242) if (ToDeleteOrNotToDelete == 0) { 243) cout << "\nRecord saved." << endl; 244) break; } 245) else if ( (ToDeleteOrNotToDelete != 1) && (ToDeleteOrNotToDelete == 0) ) 246) cout << "Invalid input." << endl; 247) } 248) } 249) 250) if (SearchResults == 0) 251) cout << SearchFor << " Not Found" << endl; 252) } 253) 254) void InventoryRecordVector::RemoveRecordsByLocation() //This function searches for records by the location field and deletes them. 255) { 256) string SearchFor; 257) int SearchResults, ToDeleteOrNotToDelete; 258) 259) cin.ignore(10, '\n'); 260) cout << "\nEnter Search Value: "; 261) getline(cin, SearchFor); 262) 263) for (RecordListIterator = RecordList.begin() ; RecordListIterator != RecordList.end() ; RecordListIterator++) 264) { 265) if(RecordListIterator->GetLocation() == SearchFor) 266) { 267) RecordListIterator->DisplayRecord(); 268) SearchResults++; 269) cout << "\nWould you like to delete this record? Enter 1 if yes and 0 if no: "; 270) cin >> ToDeleteOrNotToDelete; 271) if (ToDeleteOrNotToDelete == 1) { 272) RecordList.erase(RecordListIterator); 273) cout << "\nRecord erased." << endl; } 274) if (ToDeleteOrNotToDelete == 0) { 275) cout << "\nRecord saved." << endl; 276) break; } 277) else if ( (ToDeleteOrNotToDelete != 1) && (ToDeleteOrNotToDelete == 0) ) 278) cout << "Invalid input." << endl; 279) } 280) } 281) 282) if (SearchResults == 0) 283) cout << SearchFor << " Not Found" << endl; 284) } 285) 286) void InventoryRecordVector::RemoveRecordsByTotal() //This function searches for records by the quantity field and deletes them. 287) { 288) int SearchFor, SearchResults, ToDeleteOrNotToDelete; 289) 290) cin.ignore(10, '\n'); 291) cout << "\nEnter Search Value: "; 292) cin >> SearchFor; 293) 294) for (RecordListIterator = RecordList.begin() ; RecordListIterator != RecordList.end() ; RecordListIterator++) 295) { 296) if(RecordListIterator->GetTotal() == SearchFor) 297) { 298) RecordListIterator->DisplayRecord(); 299) SearchResults++; 300) cout << "\nWould you like to delete this record? Enter 1 if yes and 0 if no: "; 301) cin >> ToDeleteOrNotToDelete; 302) if (ToDeleteOrNotToDelete == 1) { 303) RecordList.erase(RecordListIterator); 304) cout << "\nRecord erased." << endl; } 305) if (ToDeleteOrNotToDelete == 0) { 306) cout << "\nRecord saved." << endl; 307) break; } 308) else if ( (ToDeleteOrNotToDelete != 1) && (ToDeleteOrNotToDelete == 0) ) 309) cout << "Invalid input." << endl; 310) } 311) } 312) 313) if (SearchResults == 0) 314) cout << SearchFor << " Not Found" << endl; 315) } 316) 317) void InventoryRecordVector::ShowRecords() //This member function will display the entire contents of the vector. 318) { 319) if (RecordList.empty()) 320) { 321) cout << "\nThe list has no elements." << endl; 322) return; 323) } 324) for(RecordListIterator = RecordList.begin() ; ; RecordListIterator++) 325) { 326) RecordListIterator->DisplayRecord(); 327) if (RecordListIterator == RecordList.end()) 328) { 329) cout << "\nSearch done." << endl; 330) break; 331) } 332) } 333) } 334) 335) void InventoryRecordVector::OutputListToFile() 336) { 337) fstream OutputToFile("List", ios::out); 338) 339) for(RecordListIterator = RecordList.begin() ; ; RecordListIterator++) 340) { 341) OutputToFile << RecordListIterator->GetDesc() << endl; 342) OutputToFile << RecordListIterator->GetID() << endl; 343) OutputToFile << RecordListIterator->GetCost() << endl; 344) OutputToFile << RecordListIterator->GetLocation() << endl; 345) OutputToFile << RecordListIterator->GetTotal() << endl; 346) if (RecordListIterator == RecordList.end()) 347) { 348) cout << "\nSearch done." << endl; 349) break; 350) } 351) } 352) 353) OutputToFile.close(); 354) 355) cout << "\nFile saved." << endl; 356) } 357) 358) void InventoryRecordVector::LoadListFromFile() //This member function loads the contents of the vector from the contents of a file. 359) { 360) fstream InputFromFile("List", ios::in); 361) 362) for ( ; ; ) 363) { 364) InventoryRecord RecordName; 365) 366) getline(InputFromFile, DescTemp); 367) RecordName.SetDesc(DescTemp); 368) 369) getline(InputFromFile, IDTemp); 370) RecordName.SetID(IDTemp); 371) 372) getline(InputFromFile, CostTemp); 373) RecordName.SetCost(CostTemp); 374) 375) getline(InputFromFile, LocationTemp); 376) RecordName.SetLocation(LocationTemp); 377) 378) getline(InputFromFile, TotalTemp); 379) RecordName.SetTotal(TotalTemp); 380) 381) RecordList.push_back(RecordName); 382) 383) if(InputFromFile.eof()) //This is true just in case the program 384) break; // is at the end of the file... 385) } 386) 387) InputFromFile.close(); 388) 389) cout << "\nFile loaded." << endl; 390) } 391) 392) void InventoryRecordVector::NewList() 393) { 394) RecordList.clear(); 395) cout << "\nNew empty list created." << endl; 396) } 397) 398) 399) 400) 401) 402) 403) 404) 405) 406) 407) 408) 409) 410) 411) 412) 413) 414) 415) 416) 417) 418) 419) 420) void Menu(); //Declaring and defining the main functions of the program. 421) int MenuResults(string Cmd, int M, InventoryRecordVector List); 422) void RemoveRecords(InventoryRecordVector List); 423) 424) int main() //Finally, the main program! 425) { 426) InventoryRecordVector List; 427) int M=0; 428) string Cmd; 429) 430) while(true) 431) { 432) Menu(); 433) cout << "\nSelect command: "; 434) getline(cin, Cmd); 435) 436) if(Cmd == "Quit" || Cmd == "quit") 437) break; 438) 439) M = MenuResults(Cmd, M, List); 440) if (M == 2) 441) RemoveRecords(List); 442) if (M == 0) 443) cout << "\nInvalid command." << endl; 444) } 445) } 446) 447) void Menu() //Displays the menu of the program. 448) { 449) cout << "\n\n\n========================================================"; 450) cout << "\nChoices:" << endl; 451) cout << "Add\t\tAdd records to the list" << endl; 452) cout << "Find\tSearch for a particular record" << endl; 453) cout << "Remove\tDelete a record from the list" << endl; 454) cout << "Show\tDisplay all the records on the list" << endl; 455) cout << "Save\tSave list as a plain-format ASCII file" << endl; 456) cout << "Load\tLoad list from a previously-saved ASCII file and add contents to end of current list" << endl; 457) cout << "New\t\tClear current list and restart with a new list" << endl; 458) cout << "Quit" << endl; 459) cout << "========================================================"; 460) } 461) 462) int MenuResults(string Cmd, int M, InventoryRecordVector List) 463) { 464) M=0; 465) 466) if(Cmd == "Add" || Cmd == "add") 467) { 468) M=1; 469) List.AddRecords(); 470) } 471) if(Cmd == "Find" || Cmd == "find") 472) { 473) M=1; 474) List.FindRecord(); 475) } 476) if(Cmd == "Remove" || Cmd == "remove") 477) { 478) M=2; 479) } 480) if(Cmd == "Show" || Cmd == "show") 481) { 482) M=1; 483) List.ShowRecords(); 484) } 485) if(Cmd == "Save" || Cmd == "save") 486) { 487) M=1; 488) List.OutputListToFile(); 489) } 490) if(Cmd == "Load" || Cmd == "load") 491) { 492) M=1; 493) List.LoadListFromFile(); 494) } 495) if(Cmd == "New" || Cmd == "new") 496) { 497) M=1; 498) List.NewList(); 499) } 500) return M; 501) } 502) 503) void RemoveRecords(InventoryRecordVector List) //Displays the submenu used to remove objects from the vector. 504) { 505) string Choice; 506) int L; 507) 508) cout << "\n========================================================"; 509) cout << "\nWays to remove records:" << endl; 510) cout << "1\tRemove by ID." << endl; 511) cout << "2\tRemove by description." << endl; 512) cout << "3\tRemove by cost." << endl; 513) cout << "4\tRemove by location." << endl; 514) cout << "5\tRemove by quantity." << endl; 515) cout << "6\tQuit" << endl; 516) cout << "========================================================"; 517) cout << "\n\nPlease enter your choice, then hit enter twice: "; 518) 519) getline(cin, Choice); 520) 521) 522) if (Choice == "1" || Choice == "ID" || Choice == "id") 523) { 524) L++; 525) List.RemoveRecordsByID(); 526) } 527) if (Choice == "2" || Choice == "description" || Choice == "Description") 528) { 529) L++; 530) List.RemoveRecordsByDescription(); 531) } 532) if (Choice == "3" || Choice == "cost" || Choice == "Cost") 533) { 534) L++; 535) List.RemoveRecordsByCost(); 536) } 537) if (Choice == "4" || Choice == "location" || Choice == "Location") 538) { 539) L++; 540) List.RemoveRecordsByLocation(); 541) } 542) if (Choice == "5" || Choice == "quantity" || Choice == "Quantity") 543) { 544) L++; 545) List.RemoveRecordsByTotal(); 546) } 547) if (Choice == "6" || Choice == "quit" || Choice == "Quit") 548) return; 549) if (L == 0) 550) cout << "Invalid choice, back to main menu." << endl; 551) }