| 48 | }; |
| 49 | |
| 50 | void F_First_Program() |
| 51 | { |
| 52 | |
| 53 | Person p; // creating an object of a class Person |
| 54 | |
| 55 | for (int k = 0; k < 1000; k++) |
| 56 | { |
| 57 | system("cls"); |
| 58 | cout << " C O N T A C T S\n" |
| 59 | << "------------------\n" |
| 60 | << " 1. Add a contact\n" |
| 61 | << " 2. Contacts\n" |
| 62 | << " 0. Back\n" |
| 63 | << " Your choice: \n"; |
| 64 | |
| 65 | string name, tell_number; // contact info which will be inputted by user |
| 66 | |
| 67 | switch (_getch()) |
| 68 | { |
| 69 | // case 49 is for adding a new contact into a list |
| 70 | case 49: |
| 71 | { |
| 72 | system("cls"); |
| 73 | |
| 74 | ofstream out_contacts("contacts", ios::binary | ios::app); // creating a file | binary file | append mode |
| 75 | |
| 76 | cout << " ADD TO CONTACT \n-----------------------------\n Enter a contact information.\n\n"; |
| 77 | cout << " Enter the name: "; |
| 78 | cin >> name; |
| 79 | cout << " Enter the phone number: "; |
| 80 | cin >> tell_number; |
| 81 | |
| 82 | p.setData(name, tell_number); // setting inputted data |
| 83 | |
| 84 | // storing the data in file |
| 85 | out_contacts.write((char *)&p, sizeof(Person)); |
| 86 | out_contacts.close(); // closing the file |
| 87 | |
| 88 | cout << "\n Successfully added!\n\n"; |
| 89 | |
| 90 | system("pause"); |
| 91 | } |
| 92 | break; |
| 93 | |
| 94 | // case 50 lists all Contacts |
| 95 | case 50: |
| 96 | { |
| 97 | system("cls"); |
| 98 | |
| 99 | ifstream in_contacts("contacts", ios::binary); // getting data from the file |
| 100 | |
| 101 | cout << " ALL CONTACTS\n---------------------------\n"; |
| 102 | cout << " Name Phone\n---------------------------\n"; |
| 103 | |
| 104 | while (in_contacts.read((char *)&p, sizeof(Person))) |
| 105 | { |
| 106 | // displaying the data |
| 107 | p.displayData(); |
no test coverage detected