•
•
•
•
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
![]() |
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:
cplusplus 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 3:29 pm. Reason: Added code tags, please do it yourself next time.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation:
Rep Power: 36
Solved Threads: 860
>>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)!
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
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 8:45 pm. Reason: Added code tags. Use them the next time you post code.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation:
Rep Power: 36
Solved Threads: 860
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
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 9:53 pm. Reason: Added code tags. Use them the next time you post code.
•
•
Join Date: Jan 2008
Posts: 1,403
Reputation:
Rep Power: 6
Solved Threads: 179
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation:
Rep Power: 36
Solved Threads: 860
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.
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation:
Rep Power: 36
Solved Threads: 860
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
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 (Gadgets and Gizmos)
Other Threads in the C++ Forum
- Previous Thread: Creating a game Database
- Next Thread: Linked List Problem



Linear Mode