Phonebook program help

Reply

Join Date: Mar 2005
Posts: 1
Reputation: camduthie is an unknown quantity at this point 
Solved Threads: 0
camduthie camduthie is offline Offline
Newbie Poster

Phonebook program help

 
0
  #1
Apr 12th, 2005
I am trying to write a phonebook program that uses a dat file to store the numbers (ex. in dat file--John Doe 5554445555). I have it to the point when the user keys "John" in, it will display the number. If the user keys in "John Doe", it will not work. I am entry level programming so the pointer will not move back to the top of the file unless the program is closed and restarted (which is fine for this assignment). I just need to to work with keying in John Doe....Please help if you can (Be sure to create a phone.dat file to run properly)! Thanks!
  1. {
  2.  
  3. // Open file
  4. ifstream phonebook;
  5.  
  6. string nameToLookUp;
  7.  
  8. string firstName;
  9. string lastName;
  10. string phoneNumber;
  11. char runAgain;
  12.  
  13. firstName="";
  14. lastName="";
  15.  
  16. runAgain = 'y';
  17.  
  18. phonebook.open("Phone.dat");
  19. if ( !phonebook ) { //If input doesn't open...
  20. //no
  21. cout << "Can't open the phonebook file." << endl;
  22. system("PAUSE");
  23. return 1; //terminate the program
  24. }
  25.  
  26.  
  27. while (runAgain=='y'||runAgain=='Y') {
  28.  
  29. phonebook.close();
  30. phonebook.open("Phone.dat");
  31. cout<<"What Name to look up? ";
  32. cin >> nameToLookUp;
  33. while (firstName!=nameToLookUp&&lastName!=nameToLookUp&&!phonebook.eof()) {
  34. phonebook >> firstName >> lastName >> phoneNumber;
  35. }
  36.  
  37. if (!phonebook.eof())
  38. cout << phoneNumber << endl;
  39. else
  40. cout << "Could not find the phone number" << endl;
  41.  
  42. cout << "Run again? ";
  43. cin >> runAgain;
  44. }
  45.  
  46. phonebook.close();
  47. system("pause");
  48. return (0);
  49. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Phonebook program help

 
0
  #2
Apr 13th, 2005
Try something more like this:
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. cout<<"An empty string or EOF will stop the program"<<endl;
  12.  
  13. for ( ; ; ) {
  14. string name;
  15.  
  16. cout<<"Enter a name to search for: ";
  17. if ( !getline ( cin, name ) || name.empty() )
  18. break;
  19.  
  20. ifstream phonebook ( "phone.dat" );
  21.  
  22. if ( !phonebook ) {
  23. cerr<<"Error opening file"<<endl;
  24. return EXIT_FAILURE;
  25. }
  26.  
  27. vector<string> lines;
  28. string line;
  29.  
  30. while ( getline ( phonebook, line ) ) {
  31. if ( line.find ( name ) )
  32. lines.push_back ( line );
  33. }
  34.  
  35. if ( lines.empty() )
  36. cout<<"No records found"<<endl;
  37. else if ( lines.size() == 1 )
  38. cout<<"One record found"<<endl;
  39. else
  40. cout<<"Multiple records found"<<endl;
  41.  
  42. for ( vector<string>::size_type i = 0; i < lines.size(); i++ )
  43. cout<< lines[i] <<endl;
  44. }
  45.  
  46. return EXIT_SUCCESS;
  47. }
This way you can not only search by first name, last name, or both, multiple occurances of the name entered will be listed, rather than just the first one found.

Notice how I used getline to read an entire line and then process it in memory. In the end, that's the easiest way to write robust and error free I/O, and the primary lesson you'll get from the code I showed you.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC