a simple c++ program for an adress book that adds contact, stores the contacts and searches for the contact... it should have only one class called class addy_book..i called a constructor and am stuck coz i don't know where to go from here

#include <iostream>
#include <fstream>

using namespace std;

class addy_book {
   private:
     char*fname;
     char*lname;
     double fone_num;
     char sex;
public: 
     addy_book();
     add_to();
     view();
     search();
     };

Dani AI

Generated

Good start. A few design fixes will save you a lot of pain. As hinted, define exactly what each method does, and as said, separate planning from typing. Keep a single addy_book object alive for the whole run; do not create new ones inside menu() or you will lose all contacts. Avoid I/O in the constructor. Use std::string for names and phone (numbers like 0123 or +1-555-... are not doubles). Give your functions real return types (void, bool, etc.), not char*.

Here is a compact, single-class approach you can drop in and build on:

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

class addy_book {
    struct Contact { std::string first, last, phone; char sex; };
    std::vector<Contact> entries;

public:
    void add_to(const std::string& first,
                const std::string& last,
                const std::string& phone,
                char sex) {
        entries.push_back({first, last, phone, sex});
    }

    void view(std::ostream& out) const {
        for (const auto& c : entries)
            out << c.first << ' ' << c.last
                << " [" << c.phone << "] " << c.sex << '\n';
    }

    const Contact* search(const std::string& key) const {
        auto it = std::find_if(entries.begin(), entries.end(),
            [&](const Contact& c){
                return c.first == key || c.last == key ||
                       (c.first + " " + c.last) == key;
            });
        return it == entries.end() ? nullptr : &*it;
    }
};

Wire your menu to call add_to, view(std::cout), and search. For persistence, add bool save(const std::string&) and bool load(const std::string&) that write/read one contact per line (CSV or whitespace) using std::ofstream/std::ifstream. Finally, test incrementally: add two contacts, view, then search by last name and full name. This matches ’s advice: start coding the functions, but only after the plan is clear.

Recommended Answers

All 5 Replies

Is that all of the instructions you were given? I'm sort of surprised that your instructor didn't explain in greater detail what each of those methods were supposed to do.

i am supposed to build on it besides and determine what each method is suppised to do.... the addy_book is the constuctor function, add_to,...which adds new contact or update a contact, view shows what you have stored in the address book, and search helps you search through a particular database

Well then, Start writing the functions :) .or refer your textbook.

this is what i have so far

#include <iostream>
#include <string>
using namespace std;

class addy_book {

      private:
              char fname[20];
              char lname[20];
              char sex ;
              double fone_num;
     public:
            addy_book();
            int menu();
            int option;
            char* add_contacts();
            char* view_contacts();
            char* search_contacts();
            char* address_users[10];  
            int i;
};
int addy_book::menu() {

         cout<<"\t\t -------------------- MENU ------------------"<<endl;
         cout<<"\n\t\t\t\t1. add to contacts "<<endl;
         cout<<"\n\t\t\t\t2. view contacts in address book"<<endl;
         cout<<"\n\t\t\t\t3. search through the contacts in address book"<<endl;
         cout<<"\n\n\nENTER OPTION : "<<endl;
         cin>> option;


         if(option == 1) {

                   addy_book a;
                   a.add_contacts();

         }else if(option == 2) { 

               addy_book a;
               a.view_contacts();

         }else if(option == 3) {

               addy_book a;
               a.search_contacts();
         }

} 

addy_book::addy_book() {

     cout <<"enter your first name \n" ;
     cin >> fname;

     cout <<"enter your last name \n" ;
     cin >> lname;

     cout <<" choose your sex (m or f) \n" ;
     cin >> sex;

     cout <<"enter your phone number\n" ;
     cin >> fone_num;
}



char* addy_book::add_contacts() {

      cout<<"\t\t -------------------- Add Contacts ------------------"<<endl;
      cout << "enter the contcats in this format: \n" ;
           for ( i = 0; i < 10; i++)
            {
                 cin >> address_users[i];
            }

            cout<<"PRESS '1' TO GO BACK TO THE MAIN MENU"<<endl;
            cin >> option;

 } 

char* addy_book::view_contacts() {  

            cout<<"\t\t -------------------- view contacts ------------------"<<endl; 
            cout << "these are the contacts in the address book\n" ;
             for ( i = 0; i < 10; i++)
            {
                 cin >> address_users[i];
            }

            cout<<"PRESS '1' TO GO BACK TO THE MAIN MENU"<<endl;
            cin >> option;

} 

char* addy_book::search_contacts() {  

      char* target;
      int result;
      char ans; 

             cout<<"\t\t -------------------- search through contacts ------------------"<<endl; 
             cout << "search for a specific contact here \n";

             do 
             {
                  cout <<" enter a name to search for: ";
                  cin >> target;

                  result = search(address_users, target);
                     if( result == -1) {
                         cout <<target<< " is not on the list of contacts. \n" ;

                     }else{
                           cout <<target<< " is on the list and here are the details\n: \n" <<result<<endl
                           }

                       cout << "search again? (choose 'y'or 'n'): ";
                       cin ans;

             } while ((ans != 'n') && (ans != 'N'));

      }



 main () {

     cout << "welcome to zeus address system \n" ;

        int z;
        addy_book b;


        if(z != 0){
             b.menu(z);
              }else{
               cout<<"\n\nProgram Exiting . . .\n";      
         }


system("PAUSE");
return 0;
}

and i am confused at the moment can you tell me what is wrong or what i should do

1) Turn the computer off.
2) Sit at a desk with pen and paper
3) Write down the functions you need to create (add, delete, search, ...)
4) Take each one at a time and write the steps needed to accomplish them -- in great detail
5) Run through the steps a line at a time adding, deleting, etc. untill you know what you wrote works well.
6) Turn on the computer
7) Translate what you have into code.

This is the best, fastest, easiest way to program. 80% at a desk, 20% at the computer.

Think of anything that is to be created -- building, car, whatever. How much time do you imagine is used designing the thing vs. actually making it? The same with programs. More planning and design than typing code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.