phonebook problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

phonebook problem

 
0
  #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 4:29 pm. Reason: Added code tags, please do it yourself next time.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: phonebook problem

 
0
  #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)!
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

 
0
  #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 9:45 pm. Reason: Added code tags. Use them the next time you post code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: phonebook problem

 
0
  #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;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

 
0
  #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 Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

 
0
  #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 10:53 pm. Reason: Added code tags. Use them the next time you post code.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: phonebook problem

 
0
  #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 10:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: phonebook problem

 
0
  #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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 51
Reputation: code12 is an unknown quantity at this point 
Solved Threads: 0
code12's Avatar
code12 code12 is offline Offline
Junior Poster in Training

Re: phonebook problem

 
0
  #9
Mar 21st, 2008
please can you help how to make it accept more than one character
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: phonebook problem

 
0
  #10
Mar 21st, 2008
See the example I posted in post #4 of this thread.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC