Thread: Making an address book in C++ with arrays inside of structures?

  1. #1
    Registered User
    Join Date
    Sep 2009

    Question Making an address book in C++ with arrays inside of structures?

    I am trying to make a program that

    1) Ask how many positions the user would like
    2) Prompt a list of address book questions for them to fill in the data
    3) ask which position to view
    4) show all of the information in that position

    Basically I am thinking of making an array. They input how many elements to make in it. it then asks a list of questions I made in a structure and then asks which element to view and when they pick an element it shows the entire thing. This is what I have so far:

    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main(int argc, char *argv[])
        int entries;
        cout<<"How many entries would you like?  ";
     struct person
                string phoneNumber;
                string wholeName;
               /* string address;
                string DOB;
                string eMail;
                string favoriteShow;
                string city;
                string state;
                string zip;
                string myspace;
                string favoriteFood;
                string gamerTag;
                string hairColor;
                string organDonar;
                string salary;
                string employer;
                string placeOfBirth;
                string mothersMaiden;
                string teacher; 
            */     };
        int i;
        for (i=0; i<entries; i++)
        int myArray[entries]; 
                person info;
                cout<<"Enter Phone Number:  ";
                cout<<"Enter Whole Name:  ";
        return EXIT_SUCCESS;
    As you can see for now I am just working with the first two entries in the structure to see if I can get it to work.

    I think I am on the right track but

    struct myArray[entries];

    gives me an error and only works when it is

    int myArray[entries];

    but shouldn't it be struct since I am putting a structure into the array and not an integer?

    Please help!

    I would love for someone to just do the entire thing for me so I can learn from it and don't have to beat my head against the wall anymore, but any help is appreciated. =]

  2. #2
    Registered User
    Join Date
    Apr 2006
    It should be "struct person myArray[entries]" or just "person myArray[entries]". However, that's still a compiler extension to allow stack arrays of variable size.

    Instead you should use a vector<person>, and use the methods documented here to add elements.

    There are a number of other errors that jump out:
    1) Common convention is to define structs in the global scope. It allows you to have multiple functions use the same struct.
    2) whether an array or vector, the container of entries should be outside your for loop. Otherwise you're creating a new array each time.
    3) Avoid system("pause"). Use a combination of cin.ignore() and cin.get() to pause the program instead.

    Also, you really should learn object oriented programming, instead of working with structures.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Another possible solution for beginners besides a vector is to have a max size set as a constant. You can then use that max size for your array declaration and keep a separate variable to store the number of actual entries. You just have to make sure the number of entries is never greater than the max size.

    Note that the instructions in the assignment usually make it clear whether this approach is appropriate. You can also ask your instructor if there is some maximum number of entries allowed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning arrays of structures
    By manolo21 in forum C Programming
    Replies: 2
    Last Post: 03-31-2003, 08:09 AM
  2. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  3. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  4. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:48 PM
  5. Structures Arrays and Char Strings
    By Crocksmyworld in forum C Programming
    Replies: 5
    Last Post: 01-19-2002, 11:56 PM

Tags for this Thread