Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- void writeNewContact() {
- ofstream contact;
- string name;
- string number;
- contact.open("Names.txt", std::ios_base::app);
- cout << "Enter the name of new contact: ";
- cin >> name;
- cout << "\nEnter the number of " << name << ": ";
- cin >> number;
- cout << "Saved!" << endl;
- system("cls");
- contact.eof();
- contact << name << endl;
- contact.close();
- contact.open("PhoneBook.txt", std::ios_base::app);
- contact.eof();
- contact << number << endl;
- contact.close();
- }
- void getNumber(int num) {
- num = num - 1;
- ifstream example;
- example.open("PhoneBook.txt");
- string line;
- for (int i = 0; !example.eof(); i++)
- {
- getline(example, line);
- if (i == num)
- cout << line << endl;
- }
- }
- void getNames() {
- int numberForName;
- ifstream example;
- example.open("Names.txt");
- string line;
- for (int i = 0; !example.eof(); i++)
- {
- getline(example, line);
- cout << i + 1 << ") " << line << "; " << endl;
- }
- cin >> numberForName;
- example.close();
- getNumber(numberForName);
- }
- void menu() {
- while (true) {
- cout << "1) Look for number." << endl;
- cout << "2) Add new number." << endl;
- int menu;
- cin >> menu;
- system("cls");
- switch (menu)
- {
- case 1:
- getNames();
- break;
- case 2:
- writeNewContact();
- break;
- default:
- break;
- cin >> menu;
- }
- cout << "Do you wish to exit? " << endl;
- string exit;
- cin >> exit;
- if (exit == "yes")
- break;
- else if (exit == "no")
- continue;
- system("cls");
- }
- }
- int main() {
- menu();
- cin.get();
- cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement