473,382 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {  
  9.      string line;
  10.       ifstream file("a7N.txt");
  11.  
  12.   if (file.is_open())
  13.   {
  14.     while (! file.eof() )
  15.     {
  16.       getline (file,line);
  17.       cout <<line << endl;
  18.  
  19.  
  20.       }
  21.       system("PAUSE");
  22.  
  23.     ifstream file("a7N.txt");
  24.  
  25.  
  26.     char ch, fword;
  27.     string line1, num, num2,line4;
  28.     ;
  29.  
  30.     if(file >>ch>> line1 >> num >> num2 >>line4) {
  31.         cout << "[ "
  32.         << ch << ' '
  33.         << line1 << ' '
  34.         << num << ' '
  35.         <<  num2 << ' '
  36.         << line4<<" ]"
  37.         << endl;
  38.  
  39.  
  40. ;
  41.     }
  42.  
  43.     // read a line using getline
  44.     if(getline(file,line)) {
  45.         cout << "[ " << line << " ]" << endl;
  46.  
  47.         cout<<"Please enter a Capital letter?";
  48.         cin>>fword;
  49.  
  50.         if (ch == fword) 
  51.         cout << "[ "
  52.         << ch << ' '
  53.         << line1 << ' '
  54.         << num << ' '
  55.         <<  num2 << ' '
  56.         << line4<<" ]"
  57.         <<endl;    
  58.         else
  59.         cout<< "No Name starts with that letter!"<<endl;
  60.  
  61.      system("PAUSE");
  62.     return 0;
  63.  
  64.  
  65.  
  66. }
  67. }
  68. }
Nov 19 '07 #1
5 6279
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.

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. using namespace std;
  3. struct AddressBookEntry{
  4. string firstName;
  5. string lastName;
  6. string phoneNumber;    /// To support +91-080-12345678 type of numbers
  7. };
  8.  
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
Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. map<string,struct AddressBookEntry> AddressList;
  3.  
To use the map do the following.
Expand|Select|Wrap|Line Numbers
  1. struct AddressBookEntry e1;
  2. e1.firstName = "pqr";
  3. e1.lastName = "xyz";
  4. e1.phoneNumber = "1234567890";
  5. AddressList[e1.firstName] = e1;
  6.  
to retrieve do the following
Expand|Select|Wrap|Line Numbers
  1. struct AddressBookEntry retrievedEntry=AddressList[inputtedName];
  2. <use retrieved entry data here>
  3.  
Finally after you have done using the AddressList. clear the structure
Expand|Select|Wrap|Line Numbers
  1. AddressList.clear()
Nov 20 '07 #2
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(fstream &);
Nov 21 '07 #3
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?


Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. struct adressbook
  8. {
  9.      string Name;          //name of person
  10.      string Age;          //Age
  11.      string SSN;           //Social Security number
  12.      string Ad;           // address
  13. } ;
  14.  
  15. void getfunction(adressbook contact[], ifstream &file)
  16. {
  17.      int i = 0; 
  18.      string line;
  19.      if (file.is_open())
  20.      {
  21.          while (! file.eof() )
  22.          {
  23.                //read  line by line
  24.               getline (file,line);  
  25.               contact[i].Name = line; //first line is a name so store it in the array
  26.               getline (file, line); 
  27.               contact[i].Age = atoi(line.c_str()); // converting string to an int.  
  28.               getline (file, line); 
  29.               contact[i].SSN = atol(line.c_str()); // converting char to long
  30.               getline(file, line); 
  31.               contact[i].Ad = line;
  32.               getline(file, line); //read the space in between entries
  33.               i = i+1;
  34.          };
  35.          for (int k = 0; k < i; k++) //i is the number of contacts 
  36.          {  
  37.              cout << contact[k].Name << endl;
  38.              cout << contact[k].Age << endl;
  39.              cout << contact[k].SSN << endl;
  40.              cout << contact[k].Ad << endl << endl;
  41.          }
  42.      }
  43. }
  44.  
  45. int main ()
  46.      {  
  47.          int i = 0; 
  48.      adressbook contact[50];
  49.      string fword; // first letter of the name!
  50.      string line;
  51.       ifstream file("a7N.txt");
  52.  
  53.   if (file.is_open())
  54.   {
  55.     while (! file.eof() )
  56.     {
  57.       getline (file,line);
  58.       cout <<line << endl;
  59.  
  60.  
  61.       }
  62.       system("PAUSE");
  63. }
  64.  
  65. getfunction(contact,file);
  66. cout<<"Pleas e enter a Capital letter?";
  67.         cin>>fword;
  68.  
  69.     if (contact[i].Name==fword)// Searching to see if the input Letter matches the name
  70.  
  71.     cout<< line<<endl;
  72.     else
  73.     cout<< "No Name starts with that letter!"<<endl; // if no name matches then this is the output
  74.  
  75.  
  76.      system("PAUSE");
  77.          return 1;
  78.      }
  79.  
Nov 21 '07 #4
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
Expand|Select|Wrap|Line Numbers
  1.     if (contact[i].Name==fword)// Searching to see if the input Letter matches the name
  2.  
Nov 22 '07 #5
ok. Thank you very much! I will try that.
Nov 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: truckmen | last post by:
Hello All, I still can't figure it all out. I am trying to create a web page (using php) where: 1) A visitor add his or her name to an address book that can also be viewed online by other...
2
by: Phil Stanton | last post by:
Can anyone give me sone code to add addresses from a query to an Outlook Address book. The problem is that I want to add the new email addresses to a distribution list, not to the main email list ...
5
by: Ignacio Domínguez | last post by:
Hi. Is there a way of accessing the address book, similar to what Outlook Express does, using C#? Thanks Ignacio Domínguez
0
by: Bob Avallone | last post by:
MetaPro Systems Inc. Visual Studio Dot Net Tips & Tricks #3 – Direct Access to Your Outlook Address Book. Project Type: VS.NET Windows Application Code Behind: Visual Basic I have a project...
1
by: Rohan | last post by:
Hi There, I want o access address book of OUTLOOK EXPRESS on Web Clients PC so that he can select addresses from his address book & copy them to our database. Does Outlook Express support an...
11
by: MLH | last post by:
Anyone ever experiement importing WAB data directly into A97? Would lke a chance to look at any work done in this arena.
5
by: ttoboobz | last post by:
Help anyone... I'm creating a simple address book java program that would store 100 records. The GUI should be able to accept the name, address, phone no., and email add of a person by clicking a...
11
cjbrx3115
by: cjbrx3115 | last post by:
Hey guys- I just got this email from my friend. I'm not sure it will really work (it's a forward), so I'm askin' you people. Well, here it is: ...
1
by: Lpitt56 | last post by:
I am running MS Access 2007 and I want to update an Outlook Address book from my Access Database. I started out by importing the Outlook Address Book as a linked table and it linked fine. I then...
10
by: Mike Miller | last post by:
Hi, Am wanting to send email with php and need to access the global outlook address book. Are there any examples/tutorials on how to do this? M
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.