C++ help address book

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tiffrobe
    New Member
    • Nov 2007
    • 5

    C++ help address book

    Hi I am new to C++. I'm trying to figure out how to make an address book that lets you search the contents by the first name. Here is my program. It displays all the names first and then its suppose to search for the a person info by the users input. I'm very confused!
    [code=cpp]
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main()
    {
    string line;
    ifstream file("a7N.txt") ;

    if (file.is_open() )
    {
    while (! file.eof() )
    {
    getline (file,line);
    cout <<line << endl;


    }
    system("PAUSE") ;

    ifstream file("a7N.txt") ;


    char ch, fword;
    string line1, num, num2,line4;
    ;

    if(file >>ch>> line1 >> num >> num2 >>line4) {
    cout << "[ "
    << ch << ' '
    << line1 << ' '
    << num << ' '
    << num2 << ' '
    << line4<<" ]"
    << endl;


    ;
    }

    // read a line using getline
    if(getline(file ,line)) {
    cout << "[ " << line << " ]" << endl;

    cout<<"Please enter a Capital letter?";
    cin>>fword;

    if (ch == fword)
    cout << "[ "
    << ch << ' '
    << line1 << ' '
    << num << ' '
    << num2 << ' '
    << line4<<" ]"
    <<endl;
    else
    cout<< "No Name starts with that letter!"<<endl;

    system("PAUSE") ;
    return 0;



    }
    }
    }[/code]
    Last edited by sicarie; Nov 19 '07, 11:04 PM. Reason: Code tags are [code=cpp] and (your code here) [/code]. Please use them.
  • keerthiramanarayan
    New Member
    • Nov 2007
    • 13

    #2
    Hi,
    It would be easy to reply to your query if you specified the structure of address book.

    I will explain considering that your addressbook entry contains the data in the following format
    <First Name> <Last Name> <Phone Number>.

    You would be greatly helped by creating a structure that holds the data that you read from the file as follows.

    Code:
    #include <string>
    using namespace std;
    struct AddressBookEntry{
    string firstName;
    string lastName;
    string phoneNumber;    /// To support +91-080-12345678 type of numbers
    };
    Next thing to do is to store the data that you retrieve from the file into a
    data structure. To retrieve based on the first name (and you are sure that there are only unique first names) store the data into a map. This is a structure provided by the Standard Template Libary of C++.

    Declare the map as
    Code:
    #include <map>
    map<string,struct AddressBookEntry> AddressList;
    To use the map do the following.
    Code:
    struct AddressBookEntry e1;
    e1.firstName = "pqr";
    e1.lastName = "xyz";
    e1.phoneNumber = "1234567890";
    AddressList[e1.firstName] = e1;
    to retrieve do the following
    Code:
    struct AddressBookEntry retrievedEntry=AddressList[inputtedName];
    <use retrieved entry data here>
    Finally after you have done using the AddressList. clear the structure
    Code:
    AddressList.clear()

    Comment

    • tiffrobe
      New Member
      • Nov 2007
      • 5

      #3
      I had added a structure before you posted but i am little confused on the map. I'm going to research it because i don't think we have learned that yet.


      code: (text)

      1.struct addressbook
      2.{
      3. string Name[45]; //name of person
      4. string Age[2]; //Age
      5. string SSN[9]; //Social Security number
      6. string Ad[40]; // address
      7. } ;
      8. void Searchcontact(f stream &);

      Comment

      • tiffrobe
        New Member
        • Nov 2007
        • 5

        #4
        I went and redid my code using what I learned from class, but my programs keeps saying there is no name that starts with that letter even if there is?


        [code=cpp]
        #include <string>
        #include <iostream>
        #include <fstream>
        #include <cstdlib>
        using namespace std;

        struct adressbook
        {
        string Name; //name of person
        string Age; //Age
        string SSN; //Social Security number
        string Ad; // address
        } ;

        void getfunction(adr essbook contact[], ifstream &file)
        {
        int i = 0;
        string line;
        if (file.is_open() )
        {
        while (! file.eof() )
        {
        //read line by line
        getline (file,line);
        contact[i].Name = line; //first line is a name so store it in the array
        getline (file, line);
        contact[i].Age = atoi(line.c_str ()); // converting string to an int.
        getline (file, line);
        contact[i].SSN = atol(line.c_str ()); // converting char to long
        getline(file, line);
        contact[i].Ad = line;
        getline(file, line); //read the space in between entries
        i = i+1;
        };
        for (int k = 0; k < i; k++) //i is the number of contacts
        {
        cout << contact[k].Name << endl;
        cout << contact[k].Age << endl;
        cout << contact[k].SSN << endl;
        cout << contact[k].Ad << endl << endl;
        }
        }
        }

        int main ()
        {
        int i = 0;
        adressbook contact[50];
        string fword; // first letter of the name!
        string line;
        ifstream file("a7N.txt") ;

        if (file.is_open() )
        {
        while (! file.eof() )
        {
        getline (file,line);
        cout <<line << endl;


        }
        system("PAUSE") ;
        }

        getfunction(con tact,file);
        cout<<"Pleas e enter a Capital letter?";
        cin>>fword;

        if (contact[i].Name==fword)// Searching to see if the input Letter matches the name

        cout<< line<<endl;
        else
        cout<< "No Name starts with that letter!"<<endl; // if no name matches then this is the output


        system("PAUSE") ;
        return 1;
        }
        [/code]

        Comment

        • keerthiramanarayan
          New Member
          • Nov 2007
          • 13

          #5
          From what i can make out you do not have a function which iterates through the list and finds the name.
          For such a function you would need how many records you read from the file (missing in your code). You can return the count from the function.
          Secondly you need to include the following code in a for / while loop
          Originally posted by tiffrobe
          [code=cpp]
          if (contact[i].Name==fword)// Searching to see if the input Letter matches the name
          [/code]

          Comment

          • tiffrobe
            New Member
            • Nov 2007
            • 5

            #6
            ok. Thank you very much! I will try that.

            Comment

            Working...