Jump to content

HOLY MOLY i CANNOT make an address book. :(

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
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:

Quote

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

const int MaxPeople = 10;


struct person

	{

		char first;

		char last;

		char *address;

		char *number;

	};


int i = 0;


Main
#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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
You've declared your person struct wrong. It should be something like

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.

#3
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
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:

#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";

  }

}



#4
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
wow that is very much clearer, thank you for that.

and i agree the instructions are really unclear :(

#5
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
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


struct PERSON

{

   char fName[50];

   char lName[50];

   char Address[100];

};


const int MAXPEOPLE = 10; 

PERSON people[MAXPEOPLE];



#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#7
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
how would i do a header file for this?

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

:(

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
Declare your datatypes and constants in it, then include it in your code file (.c,.cpp,.cxx, whatever).

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#10
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
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???

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
Like I said, your constants, #defines, data type declarations like struct and so on, as well as helper function prototypes. The actual implementations of your helper functions should go in a separate .c/.cpp file that usually has the same name as the .h file.

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you look at these three tutorials (C++, but the idea applies to C as well), you should get an idea of how to split up a project in to header/implementation files:
http://forum.codecal...zy-boolean.html
http://forum.codecal...-fuzzy-set.html
http://forum.codecal...-fuzzy-set.html

Don't worry about understanding everything the code does, just try to get a feel for how the header file declares something exists, and the implementation files define how those things work.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog