Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: HOLY MOLY i CANNOT make an address book. :(

  1. #1
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    HOLY MOLY i CANNOT make an address book. :(

    WELLLLL its been a few weeks since i posted here because i had been doing really well and understood all the problems and assignments, THANKS TO THIS SITE......

    well i ran into a major problem, my head has been throw for a loop and it wont stop spinning,

    here is the main jist of my assignment:

    Create a project called addressBook

    Add a header file and cpp file for your project

    All of your definitions should go in the header file.

    In the header file create the definition for your structure. Call it PERSON.

    Your structure should have fields for first name, last name, address, and optionally a phone number.

    Inside the cpp file you will create the functionality for your address book

    Inside the cpp file declare a global array of 10 PERSONS to hold all of the records in your address book call it people. Use a const called MAXPEOPLE to set the size of the array. Put the const in the header file.

    You are probably going to want to declare an integer variable to keep track of where you are at in the array.

    Create functions addPerson, getPerson

    These functions should take as arguments a reference to a PERSON structure.
    The addPerson method should copy the structure passed to it to the end of the array

    the getPerson should start at array element 0 and with each successive call return the next person in the array.

    Create overloaded findPerson functions. One function should take only the persons last name

    The other function should take both the persons last and first names.
    All code for the functions should be in the cpp file

    From main write functionality that will test your address book code

    Here is what my code is so far

    Header file
    Code:
    const int MaxPeople = 10;
    
    struct person
    	{
    		char first;
    		char last;
    		char *address;
    		char *number;
    	};
    
    int i = 0;
    Main
    Code:
    #include <iostream>
    #include "header.h"
    
    int maincounter = 0;
    int getcount = 0; 
    using namespace std;
    void addPerson(struct person &add);
    void getPerson(struct person &get);
    void findPerson (struct person &find);
    void findPerson (struct person &findlast, struct person &findfirst);
    
    
    person bookArray[MaxPeople];    //This is the main "address book" array
    int main()
    {
        char *last = new char[20];
        char *first = new char[20];
        char choice;
        person add;
        person getStruct;
        
        cout << "Address Book" << endl<< endl;
    
        while (true)
        {    
            cout << " Enter 1 to add to the address book. " <<endl;
            cout << " Enter 2 to get a person. " << endl;
            cout << " Enter 3 to find a person by last name only." << endl;
            cout << " Enter 4 to find a person by last name and first. " << endl;
            cout << " Enter any other key to quit. " << endl;
            cout << endl << "Please make a selection:  ";
            cin >> choice;
    
            switch(choice)
            {
    
                case '1':
                    
                    cout << "Enter First Name" << endl;
                    cin >> add.first;
                    cout << "Enter Last Name" << endl;
                    cin >> add.last;
                    cout << "Enter Address" << endl;
                    cin.getline(add.address, 40);
                    addPerson(add);    
                    break;
    
                case '2':
                    cout << "Getting the next person in the address book: " << endl << endl;
                    getStruct = getPerson(getStruct)
                    cout << getStruct << endl;
                    break;
    
                
                case '3':
                    cout << "Please enter the last name to search for: " << endl;
                    cin >> last;
                    findPerson(last);
                    
                    break;
                
                    case '4':
                    cout << "Please enter the last name and then the first name to search for: " << endl;
                    cout << "Last name: ";
                    cin >> last;
                    cout << "First name: ";
                    cin >> first;
                    findPerson(last, first);
                    break;   
                    */
    
    
                default:
                    return 0;
                    break;
            }
        }
    return 0;
    }
    
    
    void addPerson(struct person &add) 
        {
        bookArray[maincounter]=add;
        maincounter++; 
        }
    
    void getPerson(struct person &get)
        {
            if (getcount > maincounter)
                {
                    getcount = 0;
                    get = bookArray[getcount];
                    getcount++;
                }
            else
                {
                    get = bookArray[getcount];
                    getcount++;
                }
        }
    
    
    
    
    void findPerson (struct person &find)
        {
            for (int i=0; i<=MaxPeople; i++)
            {
                if (find == person.last)
                    {
                    // Display all the data from the array location that holds the structure with the matching value.
                    }
            }
             //I have a feeling my logic is incorrect using a counter to check each array value for the last name.
        }
    
    
    
    void findPerson (struct person &findlast, struct person &findfirst)
        {
            for (int i=0; i<=MaxPeople; i++)
                {
                    if (findlast == person.last || findfirst == person.first)
                        {
                        // Display all the data from the array location that holds the structure with the matching values.
                        }
                }
    
    
                //I have a feeling my logic is incorrect using a counter to check each array value for the last and first names.
                        
        }
    i thought i was off to a good start, but it appears my looking into the eyes of god as a dead end....

    can anyone guide me through the proper way of completing this? im troubled on this project

    rep of course for help
    thank you so much
    kevin

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: HOLY MOLY i CANNOT make an address book. :(

    You've declared your person struct wrong. It should be something like

    Code:
    struct person
    	{
    		char first[32];
    		char last[32];
    		char address[40];
    		char number[16];
    	};
    Instead of declaring maxPeople as a const int, just do a #define so you don't have to worry about doing the whole extern thing.

    findPerson won't work because you can't compare structs like that using the == operator. You'll have to compare each field.

    getPerson won't work because you can't assign structs to one another using the = operator. It's not an int or char.

  4. #3
    CPD
    CPD is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    57
    Rep Power
    12

    Re: HOLY MOLY i CANNOT make an address book. :(

    You have a lot of syntax errors and type mismatches. It doesn't help that the requirements are a little confusing. Here's my version, that might help you out:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct Person {
      string first_name;
      string last_name;
      string address;
      string phone;
    };
    
    const int MAXPEOPLE = 10;
    
    Person people[MAXPEOPLE];
    int current = 0;
    int total = 0;
    
    void addPerson(const Person& p) { people[total++] = p; }
    void getPerson(Person& p) { p = people[current++]; }
    
    Person findPerson(string last_name, string first_name)
    {
      for (int i = 0; i < MAXPEOPLE; i++) {
        if (people[i].last_name == last_name &&
          (first_name == "" || people[i].first_name == first_name ) ) {
    
          return people[i];
        }
      }
    
      return Person();
    }
    
    Person findPerson(string last_name) { return findPerson(last_name, ""); }
    
    int main()
    {
      string opt;
    
      while (cout << "> ", getline(cin, opt) && opt != "quit") {
        if (opt == "add") {
          if (total < MAXPEOPLE) {
            Person p;
    
            cout << "Last name: ";
            getline(cin, p.last_name);
    
            cout << "First name: ";
            getline(cin, p.first_name);
    
            cout << "Address: ";
            getline(cin, p.address);
    
            cout << "Phone number: ";
            getline(cin, p.phone);
    
            addPerson(p);
          }
          else
            cerr << "Database is full\n";
        }
        else if (opt == "findlast") {
          string last_name;
    
          cout << "Last name: ";
          getline(cin, last_name);
          
          Person p = findPerson(last_name);
    
          cout << p.last_name << ", " << p.first_name << '\n'
              << p.address << " -- " << p.phone << '\n' << endl;
        }
        else if (opt == "findfull") {
          string last_name;
          string first_name;
    
          cout << "Last name: ";
          getline(cin, last_name);
    
          cout << "First name: ";
          getline(cin, first_name);
          
          Person p = findPerson(last_name, first_name);
    
          cout << p.last_name << ", " << p.first_name << '\n'
              << p.address << " -- " << p.phone << '\n' << endl;
        }
        else if (opt == "list") {
          current = 0;
    
          while (current < total) {
            Person p;
    
            getPerson(p);
    
            cout << p.last_name << ", " << p.first_name << '\n'
              << p.address << " -- " << p.phone << '\n' << endl;
          }
        }
        else
          cerr << "Options are (add, findlast, findfull, list, quit)\n";
      }
    }

  5. #4
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    Re: HOLY MOLY i CANNOT make an address book. :(

    wow that is very much clearer, thank you for that.

    and i agree the instructions are really unclear

  6. #5
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    Re: HOLY MOLY i CANNOT make an address book. :(

    errors are killing me
    i feel like just paying someone to do this project for me, theres no way i can get this thing done in time. ARRRRGH

    haha

    i think i have the person struct right

    Code:
    struct PERSON
    {
       char fName[50];
       char lName[50];
       char Address[100];
    };
    
    const int MAXPEOPLE = 10; 
    PERSON people[MAXPEOPLE];

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: HOLY MOLY i CANNOT make an address book. :(

    For a simple address, that looks OK. What you may want to do is put that in a blank project with a simple int main() and see if it will compile. Then add your next item and try to compile. That will help you fix errors as you find them, rather than get hundreds of error messages from one bug.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    Re: HOLY MOLY i CANNOT make an address book. :(

    how would i do a header file for this?

    the professor expects us to "just do one" but gives no class notes on it...


  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: HOLY MOLY i CANNOT make an address book. :(

    Declare your datatypes and constants in it, then include it in your code file (.c,.cpp,.cxx, whatever).

  10. #9
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: HOLY MOLY i CANNOT make an address book. :(

    The details of working with header files also depend somewhat on the compiler/IDE you are using. If you are using an IDE that supports projects, a header file is just another file in your project.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  11. #10
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    Re: HOLY MOLY i CANNOT make an address book. :(

    yeah im using visual c++, it just says in the project to include a header... im just wondering what is even supposed to go in that file???

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2011, 04:12 PM
  2. how to make data to become address
    By fantasticbag in forum Assembly
    Replies: 0
    Last Post: 12-28-2009, 04:47 PM
  3. Intel how to make data to become address
    By lam86 in forum Assembly
    Replies: 8
    Last Post: 10-29-2009, 12:47 AM
  4. Basic Address Book project with out using database.
    By rs loader in forum Visual Basic Programming
    Replies: 1
    Last Post: 09-06-2007, 10:49 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts