User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,663 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,871 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1396 | Replies: 48
Reply
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Help phonebook problem

  #1  
Mar 21st, 2008
Hi guys! I have started writing a program for phonebook which will add, delete, search, display and sort the information of a person. information will include firstname, lastname & phonenumber.
now I was testing the first part of the program which adds a contact. but my loop is not doing the work which i wsa ecpecting it to do.please look at the below code and help me find out the probelm...i wanted to add store firsatname at locatio i=0, j=0; then last name at location i=0, j=1 and the phonenumber at i=0, j=2. but my inner loop for j is working again and agian instead of just working for 3 times.
I have one more ques. is it possible to store a name(collection of characters) at location i=0, j=0 etc.. or it can just store one character.
please help the code written is below:
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. using namespace std;
  5.  
  6. const char OUTPUT_FILE[] = "out1.txt";
  7. void introMsg();
  8. void selectionMsg(int& choice);
  9. void addBlackBook(char& firstName, char& lastName, char& phonenumber, ofstream& outFile);
  10. void checkFileOpen(ofstream& aoutFile);
  11. char outputBlackBook();
  12. void deleteFromBlackBook();
  13. char searchBlackBook();
  14.  
  15. int main(){
  16. ifstream inFile;
  17. ofstream outFile;
  18. outFile.open(OUTPUT_FILE, ios::app);
  19. char firstName;
  20. char lastName;
  21. char phonenumber;
  22. int choice;
  23. int i;
  24. int j;
  25. introMsg();
  26. selectionMsg(choice);
  27. if(choice == 1){
  28. addBlackBook(firstName, lastName, phonenumber, outFile);
  29. }else if(choice == 2){
  30. deleteFromBlackBook();
  31. } else if (choice == 3){
  32. searchBlackBook(firstName, lastName);
  33. } else if (choice == 4){
  34. outFile.open("out1.txt");
  35. outputBlackBook(out);
  36. }
  37.  
  38. return 0;
  39. }
  40. void introMsg(){
  41. cout << "Welcome to your black book\n";
  42. cout << " This program keeps track of your contacts\n";
  43. cout << " You may add, delete, search, and output contacts from this book\n";
  44. }
  45.  
  46. void selectionMsg(int& choice){
  47. cout << "Type 1 to ADD a contact\n";
  48. cout << "Type 2 to DELETE a contact\n";
  49. cout << "Type 3 to SEARCH for contact\n";
  50. cout << "Type 4 to OUTPUT ALL contacts\n";
  51. cout << "Type exit to terminate the program\n";
  52. cin >> choice;
  53.  
  54. }
  55. void addBlackBook(char& firstName, char& lastName, char& phonenumber, ofstream& outFile){
  56. for(int i = 0; i <= 500; i ++){
  57. for(int j = 0; j < 3; ){
  58. cout << "Type the contacts first name:\n";
  59. cin >> firstName;
  60. j++;
  61. cout <<"Type the contacts last name:\n";
  62. cin >> lastName;
  63. j++;
  64. cout <<"Type the phonenumber\n";
  65. cin >> phonenumber;
  66. }
  67. cout << firstName << lastName << phonenumber << endl;
  68. outFile << firstName << lastName << phonenumber << endl;
  69. }
  70. }
  71. void checkFileOpen(ofstream& aOutFile){
  72. if(aOutFile.fail()) {
  73. cout << "output filed opened failed\n";
  74. exit(1);
  75. }
  76. }
  77. /**
  78. char searchBlackBook(char& fName, char& lName) {
  79.   cout << "SEARCH FOR A CONTACT\n";
  80.   cout << "Type the contacts first name\n:";
  81.   cin >> fName;
  82.   cout << "Type the contacts last name\n";
  83.   cin >> lName;
  84.   for(int i = 0; i <=500; i ++){
  85.   for(int j = 0; j < 2; j++ ){
  86.   if (fName == firstName && lName == lastName) {
  87.   cout << "fName" << "lName" << endl;
  88.   }
  89.   }
  90.   }
  91. }
  92.  
  93. char outputBlackBook(ifstream& aIn) {
  94.   cout << "OUTPUT ALL CONTACTS\n";
  95.   aIn.get();
  96. }
  97. void sortContacts() {
  98.  
  99. }
  100. void swapContacts() {
  101.  
  102. }
  103.  
  104. */
Last edited by Narue : Mar 21st, 2008 at 3:29 pm. Reason: Added code tags, please do it yourself next time.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: phonebook problem

  #2  
Mar 21st, 2008
>>but my inner loop for j is working again and agian instead of just working for 3 times.

are you talking about function addBlackBook() ? Of course it does because you told it ro run that j loop 500 times (line 56)!
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

  #3  
Mar 21st, 2008
ya i got that thanks foro telling but i still have problem becuse now i have removed the for loop with int i = 0.... Now the loop should run for 3 times until j is less than 3 but still not getting the required result..please help...
code is like this now for addBlackBook:

  1. void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile){
  2. for(int j = 0; j < 3; ){
  3. cout << "Type the contacts first name:\n";
  4. cin >> firstName[20];
  5. j++;
  6. cout <<"Type the contacts last name:\n";
  7. cin >> lastName[20];
  8. j++;
  9. cout <<"Type the phonenumber\n";
  10. cin >> phonenumber[20];
  11. }
  12. cout << firstName[20] << lastName[20] << phonenumber[20] << endl;
  13. outFile << firstName[20] << lastName[20] << phonenumber[20] << endl;
  14. selectionMsg(int& choice);
Last edited by WolfPack : Mar 21st, 2008 at 8:45 pm. Reason: Added code tags. Use them the next time you post code.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: phonebook problem

  #4  
Mar 21st, 2008
There are several problems in the code you posted. Delete lines 3 and 8 because they are incrementing j outside the normal loop counter. Then on lines 4, 7 and 10 remove the [20] so that they look like this:
cin >> firstName;
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

  #5  
Mar 21st, 2008
Was not clear y to remove line 3 and 8. if i do then it won't be like what i want to do
i want to store firstbname at loactiion0, lastname at location 1 and phonenumber at location 2. but i have seen that after storing the loop again runs. and even when i cin name like mark, it will only accept m for that y?? please please...or should i use string for name...but i wonder than how will i be able to search a contact with the help of firstname and last name...
Reply With Quote  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

  #6  
Mar 21st, 2008
i have tried to change my code a little bit.now i runa only for one time and asks user for the choice input but then instead of processing that choice it give sme segmentation error

the code is as follows:
  1. for(int j = 0; j < 3; j++ ){
  2. if(j==0){
  3. cout << "Type the contacts first name:\n";
  4. cin >> firstName[20];
  5. }else if(j==1){
  6. cout <<"Type the contacts last name:\n";
  7. cin >> lastName[20];
  8. }else{
  9. cout <<"Type the phonenumber\n";
  10. cin >> phonenumber[20];
  11. }
  12. //cout << j;
  13. }
  14. cout << firstName[20] << lastName[20] << phonenumber[20] << endl;
  15. outFile << firstName[20] << lastName[20] << phonenumber[20] << endl;
  16. selectionMsg(choice);
Last edited by WolfPack : Mar 21st, 2008 at 9:53 pm. Reason: Added code tags. Use them the next time you post code.
Reply With Quote  
Join Date: Jan 2008
Posts: 1,403
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 179
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: phonebook problem

  #7  
Mar 21st, 2008
You don't need a loop as far as I can tell. Just ask three questions in a row and assign the answers to the appropriate variables. You don't want the [20] in the code unless the last name, first name, and phone number are each only one character. If you are getting segmentation faults, you probably never allocated space for your character arrays with the "new" command before calling the function.
Last edited by VernonDozier : Mar 21st, 2008 at 9:58 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: phonebook problem

  #8  
Mar 21st, 2008
As I said in my previous post, you CAN NOT USE those character arrays like that. firstname[20] is only one character so cin will not accept more than one character.

>> if i do then it won't be like what i want to do
Then you want to do the wrong thing. Your design is flawed because you can't do it like that.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

  #9  
Mar 21st, 2008
please can you help how to make it accept more than one character
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: phonebook problem

  #10  
Mar 21st, 2008
See the example I posted in post #4 of this thread.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC