| | |
phonebook problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<string> #include<fstream> using namespace std; const char OUTPUT_FILE[] = "out1.txt"; void introMsg(); void selectionMsg(int& choice); void addBlackBook(char& firstName, char& lastName, char& phonenumber, ofstream& outFile); void checkFileOpen(ofstream& aoutFile); char outputBlackBook(); void deleteFromBlackBook(); char searchBlackBook(); int main(){ ifstream inFile; ofstream outFile; outFile.open(OUTPUT_FILE, ios::app); char firstName; char lastName; char phonenumber; int choice; int i; int j; introMsg(); selectionMsg(choice); if(choice == 1){ addBlackBook(firstName, lastName, phonenumber, outFile); }else if(choice == 2){ deleteFromBlackBook(); } else if (choice == 3){ searchBlackBook(firstName, lastName); } else if (choice == 4){ outFile.open("out1.txt"); outputBlackBook(out); } return 0; } void introMsg(){ cout << "Welcome to your black book\n"; cout << " This program keeps track of your contacts\n"; cout << " You may add, delete, search, and output contacts from this book\n"; } void selectionMsg(int& choice){ cout << "Type 1 to ADD a contact\n"; cout << "Type 2 to DELETE a contact\n"; cout << "Type 3 to SEARCH for contact\n"; cout << "Type 4 to OUTPUT ALL contacts\n"; cout << "Type exit to terminate the program\n"; cin >> choice; } void addBlackBook(char& firstName, char& lastName, char& phonenumber, ofstream& outFile){ for(int i = 0; i <= 500; i ++){ for(int j = 0; j < 3; ){ cout << "Type the contacts first name:\n"; cin >> firstName; j++; cout <<"Type the contacts last name:\n"; cin >> lastName; j++; cout <<"Type the phonenumber\n"; cin >> phonenumber; } cout << firstName << lastName << phonenumber << endl; outFile << firstName << lastName << phonenumber << endl; } } void checkFileOpen(ofstream& aOutFile){ if(aOutFile.fail()) { cout << "output filed opened failed\n"; exit(1); } } /** char searchBlackBook(char& fName, char& lName) { cout << "SEARCH FOR A CONTACT\n"; cout << "Type the contacts first name\n:"; cin >> fName; cout << "Type the contacts last name\n"; cin >> lName; for(int i = 0; i <=500; i ++){ for(int j = 0; j < 2; j++ ){ if (fName == firstName && lName == lastName) { cout << "fName" << "lName" << endl; } } } } char outputBlackBook(ifstream& aIn) { cout << "OUTPUT ALL CONTACTS\n"; aIn.get(); } void sortContacts() { } void swapContacts() { } */
Last edited by Narue; Mar 21st, 2008 at 4:29 pm. Reason: Added code tags, please do it yourself next time.
>>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)!
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.
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:
code is like this now for addBlackBook:
cpp Syntax (Toggle Plain Text)
void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile){ for(int j = 0; j < 3; ){ cout << "Type the contacts first name:\n"; cin >> firstName[20]; j++; cout <<"Type the contacts last name:\n"; cin >> lastName[20]; j++; cout <<"Type the phonenumber\n"; cin >> phonenumber[20]; } cout << firstName[20] << lastName[20] << phonenumber[20] << endl; outFile << firstName[20] << lastName[20] << phonenumber[20] << endl; 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.
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.
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...
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...
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:
the code is as follows:
cpp Syntax (Toggle Plain Text)
for(int j = 0; j < 3; j++ ){ if(j==0){ cout << "Type the contacts first name:\n"; cin >> firstName[20]; }else if(j==1){ cout <<"Type the contacts last name:\n"; cin >> lastName[20]; }else{ cout <<"Type the phonenumber\n"; cin >> phonenumber[20]; } //cout << j; } cout << firstName[20] << lastName[20] << phonenumber[20] << endl; outFile << firstName[20] << lastName[20] << phonenumber[20] << endl; 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.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
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.
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.
>> 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.
![]() |
Similar Threads
- sequential search (cannot import string functions) (Python)
- problem with phonebook program (C)
- Phonebook program! (C)
- Making searching faster (Java)
- Can't setEnable a button on my Frame (Java)
- No idea how to do this problem... (C++)
- i have a data cable motorola T720 i need a software ringtone (Cellphones, PDAs and Handheld Devices)
Other Threads in the C++ Forum
- Previous Thread: Creating a game Database
- Next Thread: Linked List Problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






