Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Book
  5. {
  6. public:
  7.     std::string name;
  8.     std::string mobile_number;
  9.  
  10. public:
  11.     Book()
  12.     {
  13.         name = "";
  14.         mobile_number = "";
  15.     }
  16.  
  17.     bool add(std::string pName, std::string pMobile_number)
  18.     {
  19.         if (name == "")
  20.         {
  21.             name = pName;
  22.             mobile_number = pMobile_number;
  23.             return 1;
  24.         }
  25.         else
  26.             return 0;
  27.     };
  28. };
  29.  
  30.  
  31. int main()
  32. {
  33.     std::string temp_name;
  34.     std::string temp_mobile;
  35.     Book person[2];
  36.     bool condition = true;
  37.     int choice;
  38.     bool added = false;
  39.  
  40.     while (condition == true)
  41.     {
  42.         std::cout << "****************************" << std::endl;
  43.         std::cout << "1. Add contact" << std::endl;
  44.         std::cout << "2. Show contacts" << std::endl;
  45.         std::cout << "3. Exit program" << std::endl;
  46.         std::cout << "Enter your choice and press enter: ";
  47.  
  48.         std::cin >> choice;
  49.  
  50.         switch (choice)
  51.         {
  52.         case 1:
  53.             std::cout << "Give contact name: " << std::endl;
  54.             std::cin >> temp_name;
  55.             std::cout << "Give the mobile number: " << std::endl;
  56.             std::cin >> temp_mobile;
  57.  
  58.             for (int i = 0; i < 2; i++) {
  59.  
  60.                 if (person[i].add(temp_name, temp_mobile))
  61.                 {
  62.                     std::cout << "Contact added successfully" << std::endl;
  63.                     added = true;
  64.                     break;
  65.                 }
  66.             }
  67.                 if (added == false)
  68.                 {
  69.                     std::cout << "Memory full! Delete some contacts first" << std::endl;
  70.                     break;
  71.                 }
  72.             break;
  73.  
  74.         case 2:
  75.             for (int i = 0; i < 2; i++)
  76.             {
  77.                 std::cout << "Name: " << person[i].name << " - " << "Mobile number: " << person[i].mobile_number << std::endl;
  78.             }
  79.             break;
  80.  
  81.         case 3:
  82.             std::cout << "Ending program." << std::endl;
  83.             condition = false;
  84.             break;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement