943,945 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6467
  • C++ RSS
Apr 12th, 2005
0

Phonebook program help

Expand Post »
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!
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
camduthie is offline Offline
1 posts
since Mar 2005
Apr 13th, 2005
0

Re: Phonebook program help

Try something more like this:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compiling error - help
Next Thread in C++ Forum Timeline: Window Interface in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC