Thread: Address Book

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Address Book

    Hello, this program is an address book where you caan add/view entries. I'm having a problem printing out entries. Can someone help me find out why the information isn't getting saved into the structure array?

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    struct contactinfo
    {
        string name, phonenumber;
        int age;
    };
    
    
    int menu();
    void addentry(contactinfo,int);
    void viewentry(contactinfo,int);
    
    
    int main()
    {
        menu();
        return 0;
    }
    
    
    void addentry(contactinfo contact[],int index)
    {
        char ch;
        int size=10;
    
    
        cout<<"\nEnter contact name: " ;
        getline(cin, contact[index].name,'\n'); 
        cout<<"\nEnter contact age: " ;
        cin>> contact[index].age; cin.ignore();
        cout<<"\nEnter contact phone number: ";
        getline(cin, contact[index].phonenumber,'\n');
    
    
    
    
    
    
        cout<<"\nWould you like to add another entry? [Y/N]\n";
        cin>>ch; cin.ignore();
    
    
        switch(ch)
        {
        case 'y':
            {
                if ((index+1)==size)
                {
                    cout<<"\nYour address book is full.";
                    break;
                }
                else
                    addentry(contact, (index+1));
                    
                    break;
                            
            }
        case 'n':
            menu();
            break;
        default:
            cout<<"\nInvalid Input";
            menu();
            break;
        }
        return;
    }
    
    
    void viewentry(contactinfo contact[], int size)
    {
        for (int i=0;i<size;i++)
        {
            cout<<"\n" <<contact[i].name <<endl <<contact[i].age <<endl <<contact[i].phonenumber <<endl;
            
        }
        getchar();
        
    }
    
    
    int menu()
    {
        const int size=10;
        int ch;
        int index=0;
        contactinfo contact[size];
    
    
    
    
        cout<<"Address Book\n\n\t[1]Add a new entry\n\t[2]View previous entries\n\t[3]Exit Program\n\n" ;
        cin>>ch; cin.ignore();
    
    
        switch(ch)
        {
        case 1:
            addentry(contact,index);
            break;
        case 2:
            viewentry(contact,size);
            break;
        case 3:
            return 0;
            
        default:
            cout<<"\nInvalid Input\n\n";
            menu();
            break;
    
    
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Be careful of all those recursive calls to menu. When you call menu() recursively the local variables are re-created each time you enter the function. I suggest you investigate loops and letting the functions return to their calling function when they're finished.

    Jim

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