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

Re: phonebook problem

 
0
  #11
Mar 21st, 2008
Thanks for reply but I'm really sorry to say that it doesnot work, i tried it with just cin>>firstname. it still accepts only one character may be because of the for loop. because it stores one characterof fristname at position i=0, one character of lastname at positon 1 and one caharacter of phonenumber at position 2...i guess array has to be used there..but i'm not being able to use it..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: phonebook problem

 
0
  #12
Mar 21st, 2008
as suggested before you don't need that for loop. Just ask the three questions
  1. cout << "Type the contacts first name:\n";
  2. cin >> firstName;
  3. cout <<"Type the contacts last name:\n";
  4. cin >> lastName;
  5. cout <<"Type the phonenumber\n";
  6. cin >> phonenumber;
  7. //
  8. // You also need to separate the names with a space
  9. outFile << firstName << " " << lastName << " " << phonenumber << "\n";
  10. selectionMsg(choice);
Last edited by Ancient Dragon; Mar 21st, 2008 at 11:55 pm.
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
  #13
Mar 22nd, 2008
i'm not getting where the problem is . the code is as folows as u told to do. but it's still accepting only one character..
I'm also looking at it to find where the problem still exists..
  1. #include<iostream>
  2. #include<cstring>
  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, int& choice);
  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. introMsg();
  24. selectionMsg(choice);
  25. if((choice = 1)){
  26. addBlackBook(firstName, lastName, phonenumber, outFile, choice);
  27. }/**else if(choice == 2){
  28.   deleteFromBlackBook();
  29.   } else if (choice == 3){
  30.   searchBlackBook(firstName, lastName);
  31.   } else if (choice == 4){
  32. // inFile.open("abc12.txt");
  33.   outFile.open("out1.txt");
  34.   outputBlackBook(out);
  35.   }*/
  36.  
  37. return 0;
  38. }
  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, int& choice){
  56.  
  57. // for(int j = 0; j < 3; j++ ){
  58. // if(j==0){
  59. cout << "Type the contacts first name:\n";
  60. cin >> firstName;
  61. // }else if(j==1){
  62. cout <<"Type the contacts last name:\n";
  63. cin >> lastName;
  64. // }else{
  65. cout <<"Type the phonenumber\n";
  66. cin >> phonenumber;
  67. // }
  68. //cout << j;
  69. // }
  70. cout << firstName<<" " << lastName << " " <<phonenumber << endl;
  71. outFile << firstName<<" " << lastName<<" " << phonenumber<< endl;
  72. selectionMsg(choice);
  73. }
  74.  
  75. void checkFileOpen(ofstream& aOutFile){
  76. if(aOutFile.fail()) {
  77. cout << "output filed opened failed\n";
  78. exit(1);
  79. }
  80.  
  81. }
  82. }
  83. /**
  84. char searchBlackBook(char& fName, char& lName) {
  85.   cout << "SEARCH FOR A CONTACT\n";
  86.   cout << "Type the contacts first name\n:";
  87.   cin >> fName;
  88.   cout << "Type the contacts last name\n";
  89.   cin >> lName;
  90.   for(int i = 0; i <=500; i ++){
  91.   for(int j = 0; j < 2; j++ ){
  92.   if (fName == firstName && lName == lastName) {
  93.   cout << "fName" << "lName" << endl;
  94.   }
  95.   }
  96.   }
  97. }
  98.  
  99. char outputBlackBook(ifstream& aIn) {
  100.   cout << "OUTPUT ALL CONTACTS\n";
  101.   aIn.get();
  102. }
  103. void sortContacts() {
  104. }
  105. void swapContacts() {
  106.  
  107. }
  108.  
  109. */
Last edited by Ancient Dragon; Mar 22nd, 2008 at 12:25 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: phonebook problem

 
0
  #14
Mar 22nd, 2008
lines 19 to 21 are wrong -- that is only allocating one character to each of those variables. You need to make them arrays, like this:
char lastname[20];
And line 55 is also wrong -- I think you had it right the first time
void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){
Last edited by Ancient Dragon; Mar 22nd, 2008 at 12:29 am.
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
  #15
Mar 22nd, 2008
I have edited the progarm to alloacet space for more than one character. but it's still not working.

the code is as below. here i have assumed that user will not type more than 20 letter name or number. but the loop used by me is running for 20 times and is asking for 20 characters. i wanted that user could type as many as 20 characters. if he enters only 4 letter name than even it stops and ask for the last name. but i'm not able to make it possible...

the code is as follows:
  1. void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){
  2.  
  3. cout << "Type the contacts first name:\n";
  4. for (int i=0; i< NUMBER; i++){
  5. cin >> firstName[i];
  6. }
  7. cout <<"Type the contacts last name:\n";
  8. for(int i = 0; i < NUMBER; i++){
  9. cin >> lastName[i];
  10. }
  11. cout <<"Type the phonenumber\n";
  12. for(int i = 0; i < NUMBER; i++){
  13. cin >> phonenumber[i];
  14. }
  15. cout << firstName[i] << lastName[i] << phonenumber[i] << endl;
  16. outFile << firstName[i] << lastName[i] << phonenumber[i] << endl;
  17. selectionMsg(choice);
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
  #16
Mar 22nd, 2008
above in main i have used it like this:
  1. #include<iostream>
  2. #include<cstring>
  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, int& choice);
  10. void checkFileOpen(ofstream& aoutFile);
  11. //char outputBlackBook();
  12. //void deleteFromBlackBook();
  13. //char searchBlackBook();
  14. const int NUMBER = 20;
  15. int main(){
  16. ifstream inFile;
  17. ofstream outFile;
  18. outFile.open(OUTPUT_FILE, ios::app);
  19. char firstName[NUMBER];
  20. char lastName[NUMBER];
  21. char phonenumber[NUMBER];
  22. int choice;
  23. introMsg();
  24. selectionMsg(choice);
  25. if((choice = 1)){
  26. addBlackBook(firstName, lastName, phonenumber, outFile, choice);
  27. }/**else if(choice == 2){
  28.   deleteFromBlackBook();
  29.   } else if (choice == 3){
  30.   searchBlackBook(firstName, lastName);
  31.   } else if (choice == 4){
  32. // inFile.open("abc12.txt");
  33.   outFile.open("out1.txt");
  34.   outputBlackBook(out);
  35.   }*/
  36.  
  37. return 0;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
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: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: phonebook problem

 
0
  #17
Mar 22nd, 2008
Originally Posted by code12 View Post
I have edited the progarm to alloacet space for more than one character. but it's still not working.

the code is as below. here i have assumed that user will not type more than 20 letter name or number. but the loop used by me is running for 20 times and is asking for 20 characters. i wanted that user could type as many as 20 characters. if he enters only 4 letter name than even it stops and ask for the last name. but i'm not able to make it possible...

the code is as follows:
  1. void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){
  2.  
  3. cout << "Type the contacts first name:\n";
  4. for (int i=0; i< NUMBER; i++){
  5. cin >> firstName[i];
  6. }
  7. cout <<"Type the contacts last name:\n";
  8. for(int i = 0; i < NUMBER; i++){
  9. cin >> lastName[i];
  10. }
  11. cout <<"Type the phonenumber\n";
  12. for(int i = 0; i < NUMBER; i++){
  13. cin >> phonenumber[i];
  14. }
  15. cout << firstName[i] << lastName[i] << phonenumber[i] << endl;
  16. outFile << firstName[i] << lastName[i] << phonenumber[i] << endl;
  17. selectionMsg(choice);
Why the hell did you do that???? Put that back the way it was -- without the loops and the way I showed you to do it. The cin function is pretty darned smart and doesn't need those loops to get characters from the keyboard. So stop making things worse then you had them.
Last edited by Ancient Dragon; Mar 22nd, 2008 at 1:22 am.
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
  #18
Mar 22nd, 2008
u know it still accepts only one character even if i use char firstname[20] and not use those for statements. have to tried to run that program with char firstName[20]
and without those for loops.
i agree for loops are creating mess in hte code but then something else has to be used to make cin accepts all the characters which user types as a name...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
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: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: phonebook problem

 
0
  #19
Mar 22nd, 2008
Not sure what your call is from the main function, but something like this should be your function. No loops, no array indexes. cin and cout can figure out exactly when things start and stop.

  1. void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice)
  2. {
  3. cout << "Type the contacts first name:\n";
  4. cin >> firstName;
  5.  
  6. cout <<"Type the contacts last name:\n";
  7. cin >> lastName;
  8.  
  9. cout <<"Type the phonenumber\n";
  10. cin >> phonenumber;
  11.  
  12. cout << firstName << " " << lastName << " " << phonenumber << endl;
  13. outFile << firstName << lastName << phonenumber << endl;
  14. selectionMsg(choice);
  15. }
If this doesn't work, I'm guessing that the problem is in the way you are calling the function.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
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: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: phonebook problem

 
0
  #20
Mar 22nd, 2008
Originally Posted by code12 View Post
above in main i have used it like this:
  1. #include<iostream>
  2. #include<cstring>
  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, int& choice);
  10. void checkFileOpen(ofstream& aoutFile);
  11. //char outputBlackBook();
  12. //void deleteFromBlackBook();
  13. //char searchBlackBook();
  14. const int NUMBER = 20;
  15. int main(){
  16. ifstream inFile;
  17. ofstream outFile;
  18. outFile.open(OUTPUT_FILE, ios::app);
  19. char firstName[NUMBER];
  20. char lastName[NUMBER];
  21. char phonenumber[NUMBER];
  22. int choice;
  23. introMsg();
  24. selectionMsg(choice);
  25. if((choice = 1)){
  26. addBlackBook(firstName, lastName, phonenumber, outFile, choice);
  27. }/**else if(choice == 2){
  28.   deleteFromBlackBook();
  29.   } else if (choice == 3){
  30.   searchBlackBook(firstName, lastName);
  31.   } else if (choice == 4){
  32. // inFile.open("abc12.txt");
  33.   outFile.open("out1.txt");
  34.   outputBlackBook(out);
  35.   }*/
  36.  
  37. return 0;
  38. }
Function call looks fine except you have a problem here:
  1. if((choice = 1)){
  2. addBlackBook(firstName, lastName, phonenumber, outFile, choice);
  3. }
You want == in the if statement, not =.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC