Thread: Limitless profile info in address book?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    Limitless profile info in address book?

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    struct user
    {
      string name;
      string address;
      string phone;
    
    
    };
    
    
    void open_entry(user profile[], int c);
    
    
    void add_entry(user profile[], int &i, int entry[])
    {
        std::cout << "=====ADD NEW ENTRY=====\n\n";
        std::cout << "Name: ";
        std::getline(cin,profile[i].name);
        std::cout << "Address: ";
        std::getline(cin,profile[i].address);
        std::cout << "Phone number: ";
        std::getline(cin,profile[i].phone);
    
    
        entry[i];
        i++;
    
    
    
    
    }
    
    
    void browse_entry(user profile[],int i, int entry[])
    {   std::cout << "=====BROWSE ENTRY=====\n\n";
        std::cout << "Total entries: " << i << endl;
    
    
       for (int k=1;k<entry[i];k++)
        {
            std::cout << k << ". " << profile[k-1].name << endl;
        }
    
    
    
    
        std::cout << "Select entry (1 to " << i << " )\n";
        int list;
        std::cin  >> list;
        std::cout << endl << endl;
    
    
    
    
    
    
      for (int i=0;i<10;i++){
        if(list == entry[i])
        {
            int c = entry[i];
            open_entry(profile,c);
        }
      }
    }
    
    
    
    
    void open_entry(user profile[], int c)
    {
        std::cout << "Name: " << profile[c-1].name << endl;
        std::cout << "Address: " << profile[c-1].address << endl;
        std::cout << "Phone number: " << profile[c-1].phone << endl;
    
    
    }
    
    
    int main()
    {
        bool exit_main(false);
        int i=0;
    
    
        user profile[10];
    
    
        int entry[10] = {1,2,3,4,5,6,7,8,9,10};
    
    
        do{
            std::cout << "=====MAIN=====\n";
        std::cout << endl;
        std::cout << "Select option: \n";
        std::cout << "1. Add new entry \n";
        std::cout << "2. Browse entry list \n";
        std::cout << "3. Exit \n";
        std::cout << endl;
        int option;
        std::cin >> option;
        std::cin.ignore();
        switch(option)
         {
             case 1:
             add_entry(profile, i,entry);
             break;
             case 2:
             browse_entry(profile, i,entry);
             break;
             case 3:
             exit_main = true;
             std::cout << "Exiting...\n";
             break;
             default:
             std::cout << "Invalid option. \n";
    
    
         }
    
    
        }
        while(!exit_main);
    
    
    
    
    }
    Right now I can only think of just putting a limited number of profiles. Is it possible to make it limitless, letting the user to add as many profiles as he wants?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up the standard containers (std::vector, std::list, etc) for ways to manage collections of arbitrary sizes.

    Strictly speaking, it is not possible to have no limit on the size. However, standard containers allow sizes up to what is physically and logically possible on the host system (hardware and operating system) i.e. to the limits of memory and other storage. Since all real computers have finite amounts of memory, there is always a limit.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Thanks. My book is being tricky again, asking me a question that is to be found on the next next next chapter. I thought there's was a trick to do this under current chapter.. thanks again

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just a quick note. If you aren't aware of std::array yet (which you probably aren't), then I recommend you at least get familiar with it here.
    It has a number of advantages (knowing its size, disallowing you to pass an array of a size to a function that expects a different size, bounds checking [no bounds checking can be a security risk]), so it may be worth knowing about. Just remember that it requires a TR1 or C++11 compiler (Clang, Visual C++, gcc are known supported ones).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. address book
    By cboard in forum C Programming
    Replies: 10
    Last Post: 04-05-2007, 11:47 PM
  2. Address Book
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 12-10-2002, 05:18 PM
  3. address book
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 12-01-2002, 08:19 PM
  4. Address Book
    By Granger9 in forum C Programming
    Replies: 4
    Last Post: 09-09-2002, 01:02 PM
  5. Address book
    By sundeeptuteja in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2002, 02:40 PM