CST 116 Module 9: Lab 9 16a 11.14 pg 337-338 #2, #3, #4 #2 #include #include #include using namespace std; int main() { int index = 0; int ints[10]; ifstream inputFile; string fileName; cout << "Enter the file name having 10 ints each in new line: "; cin >> fileName; inputFile.open(fileName); if (!inputFile.is_open()) { cout << "\n\nUnable to open file. Ending..\n\n\n\n"; return 0; } while (!inputFile.eof()) { inputFile >> ints[index]; index++; } inputFile.close(); int smallest = ints[0], largest = ints[0]; for (int i = 1; i < 10; i++) { if (ints[i] > largest) { largest = ints[i]; } if (ints[i] < smallest) { smallest = ints[i]; } } cout << "\nSmallest int in the file is: \n" << smallest; cout << "\nLargest int in the file is: \n" << largest; for (int i = 0; i < 10; i++) { for (int j = i + 1; j < 10; j++) { if (ints[i] > ints[j]) { int newInt = ints[j]; ints[j] = ints[i]; ints[i] = newInt; } } } cout << "\nSorted list is as below: \n\n" << endl; for (int i = 0; i < 10; i++) { cout << ints[i] << "\n"; } return 0; } #3 #include #include #include using namespace std; int main() { string fileName; cout << "Enter file name: "; cin >> fileName; ifstream in(fileName.c_str()); if (in.is_open()) { string line; int lineNumber = 1; while (getline(in, line)) { cout << lineNumber << " " << line << " " << line.size() << endl; ++lineNumber; } in.close(); } else { cout << fileName << " is not a file that exists, terminating program..." << endl; } return 0; } #4 #include #include #include #include #include #include using namespace std; struct PersonInfo { char firstname[100]; char LastName[100]; char birthdate[100]; long long Phone_number; }; struct PersonInfo persondata[100]; class Person { public: char firstname[100]; char LastName[100]; char birthdate[100]; long long Phone_number; public: void AddNewPerson(); void PrintDetails(); void Edit_Details(int index2); int Find_Person(char Name[]); public: int index; }; int Person::Find_Person(char Name[]) { for (int i = 0; i < index; i++) { if (!(strcmp(persondata[i].firstname, Name))) { cout << "\nThe Details are Found" << endl; cout << "\nFirstName: " << persondata[i].firstname << endl; cout << "\nPhone: " << persondata[i].Phone_number << endl; return i; } } return -1; } void Person::Edit_Details(int index2) { int option; cout << "\nFirstName: " << persondata[index2].firstname << endl; cout << "\nLastName: " << persondata[index2].LastName << endl; cout << "\nBirthDate: " << persondata[index2].birthdate << endl; cout << "\nPhone: " << persondata[index2].Phone_number << endl; cout << "\nSelect Any Field to Update" << endl; cin >> option; switch (option) { case 1: cout << "\nPlease Enter New Name" << endl; cin >> firstname; strcpy_s(persondata[index2].firstname, firstname); break; case 2: cout << "\nPlease Enter Last Name" << endl; cin >> LastName; strcpy_s(persondata[index2].LastName, LastName); break; case 3: cout << "\nPlease Enter Birthdate" << endl; cin >> birthdate; strcpy_s(persondata[index2].birthdate, birthdate); break; case 4: cout << "\nPlease enter Phone number" << endl; cin >> Phone_number; persondata[index2].Phone_number = Phone_number; break; } } void Person::AddNewPerson() { cout << "\nPlease enter FirstName: " << endl; cin >> persondata[index].firstname; cout << "\nPlease Enter LastName: " << endl; cin >> persondata[index].LastName; cout << "\nPlease enter Birth Date: " << endl; cin >> persondata[index].birthdate; cout << "\nPlease enter Phone Number: " << endl; cin >> persondata[index].Phone_number; cout << "\n Added New Person Details Successfully " << endl; } void Person::PrintDetails() { if (index == 0) cout << "\nEmpty" << endl; else { cout << "\nFirstName\tLastName\t\tBirthDate\tPhone_number" << endl; for (int i = 0; i < index; i++) { cout << persondata[i].firstname << "\t" << persondata[i].LastName << "\t" << persondata[i].birthdate << "\t" << persondata[i].Phone_number << endl; } } } int main() { Person obj; ifstream infile; int option, retval; obj.index = 0; char FirstName[100]; infile.open("Emp_info.txt"); ofstream outfile; for (int i = 0; i < 6; i++) { infile >> persondata[i].firstname; infile >> persondata[i].LastName; infile >> persondata[i].birthdate; infile >> persondata[i].Phone_number; obj.index++; } while (true) { cout << "\n------------------------------------------" << endl; cout << "***********Persons Information ***************" << endl; cout << "------------------------------------------" << endl; cout << "\n1. Find a person's information" << endl; cout << "\n2. Add a person to the database???" << endl; cout << "\n3. Edit a person's information" << endl; cout << "\n4. Display all records to the screen" << endl; cout << "\n5. Quit" << endl; cout << "\n Please Select any option" << endl; cin >> option; switch (option) { case 1: cout << "\nPlease enter Person FirstName: " << endl; cin >> FirstName; retval = obj.Find_Person(FirstName); if (retval == -1) cout << "\nThe Person Details not Found" << endl; break; case 2: obj.index++; obj.AddNewPerson(); cout << "\nNew Person Details Added Successfully " << endl; break; case 3: cout << "\nPlease enter Person FirstName: " << endl; cin >> FirstName; retval = obj.Find_Person(FirstName); if (retval == -1) cout << "\nThe Person Details not Found" << endl; else { obj.Edit_Details(retval); } break; case 4: obj.PrintDetails(); break; case 5: outfile.open("PersonReports.txt", ios::app); outfile << "\nFirstname" << setw(20) << "LastName" << setw(20) << "BirthDate" << setw(20) << "Phone" << endl; for (int i = 0; i < obj.index; i++) { outfile << persondata[i].firstname << setw(20) << persondata[i].LastName << setw(20) << persondata[i].birthdate << setw(20) << persondata[i].Phone_number << endl; } cout << "\nPerson Details Saved Successfully in the File (PersonReports.txt)" << endl; exit(0); break; default: cout << "wrong Option" << endl; } } }