| | |
phonebook problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I'm worried about the counter being gettin incremented because in add contact function I have stored lastName/tfirstName/tphone/n
at locations contacts[i]
where i gets incremented each time after adding these 3 informations.
BUT now when I'm trying to search it, it is showing that firstNmae is stored at location 0, lastName at location 1 and phone at location2.
which is not right.
all these three things should be at one loaction- [j]. (for loop used using j in search function)
and it should get incremented only after reading lastNmae/tfirstName/tphone/n
Please help.
at locations contacts[i]
where i gets incremented each time after adding these 3 informations.
BUT now when I'm trying to search it, it is showing that firstNmae is stored at location 0, lastName at location 1 and phone at location2.
which is not right.
all these three things should be at one loaction- [j]. (for loop used using j in search function)
and it should get incremented only after reading lastNmae/tfirstName/tphone/n
Please help.
Last edited by code12; Mar 31st, 2008 at 12:15 am.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
So i should do like thison line 27.C++ Syntax (Toggle Plain Text)
if (firstname == true && lastName == true)
and will that proble of counter be solved if i add lines 38 to 43
As far as what changing line 27 will do, try it and see! That's the best way to find out if your hunch is correct and the best way to learn to program. Experiment around. Figure out what you think the output should be, change line 27, and see if you get the right output. If you don't, look at the code some more and try something different till it works. If it works, throw some different data at the problem and see if it still works as it should.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
I'm worried about the counter being gettin incremented because in add contact function I have stored lastName/tfirstName/tphone/n
at locations contacts[i]
where i gets incremented each time after adding these 3 informations.
BUT now when I'm trying to search it, it is showing that firstNmae is stored at location 0, lastName at location 1 and phone at location2.
which is not right.
all these three things should be at one loaction- [j]. (for loop used using j in search function)
and it should get incremented only after reading lastNmae/tfirstName/tphone/n
Please help.
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
const char OUTPUT_FILE[] = "ot12.txt";
void introMsg();
void selectionMsg();
void addBlackBook( string contacts[], string fName, string lName, string phone, ofstream& outFile,int& i);
void checkFileOpen(ofstream& aoutFile);
void checkFileOpen(ifstream& aInFile);
void sortContacts(string contacts[], int NUMBER);
void swap(int& aElement1, int& aElement2);
//void deleteFromBlackBook(string fName, string lName, string firstName[], string lastName[], string phonenumber[]);
//void outputBlackBook(string contacts[],string firstName[], string lastName[], string phonenumber[], ifstream& aInFile, int& i);
void searchBlackBook(string contacts[], string foName, string loName, ifstream& aInFile);
const int NUMBER = 4;
int main(){
string fName;
string lName;
string phone;
string foName;
string loName;
string contacts[NUMBER];
ifstream inFile;
ofstream outFile;
string choice;
int i = 0;
introMsg();
do{
if(choice == "1"){
addBlackBook(contacts, fName, lName, phone,outFile, i);
outFile.close();
}else if(choice == "2"){
//deleteFromBlackBook(fName, lName, firstName, lastName, phonenumber, choice);
}else if(choice == "3"){
searchBlackBook(contacts, foName, loName, inFile);
}else if(choice == "4"){
//outputBlackBook(contacts, firstName, lastName, phonenumber, inFile, i);
}else if(choice == "exit"){
exit(1);
}else{
cout <<"you selected wrong choice\n";
}
selectionMsg();
}while(cin >> choice);
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(){
cout << "What would you like to do\n";
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";
}
void addBlackBook(string contacts[], string fName, string lName, string phone, ofstream& aOutFile, int& i ){
cout << "Type the contacts first name:\n";
cin >> fName;
cout << fName;
cout <<"Type the contacts last name:\n";
cin >> lName;
cout << lName;
cout <<"Type the phonenumber\n";
cin >> phone;
cout << phone;
if(i < NUMBER){
aOutFile.open(OUTPUT_FILE, ios::app);
checkFileOpen(aOutFile);
contacts[i] = lName + "\t" + fName + "\t" + phone;
// sortContacts(contacts, NUMBER);
aOutFile << contacts[i] << endl;
}else{
cout << "u rechd out of range\n";
aOutFile.close();
}
i++; // increments only after adding lastName/tfirstName/t phone cout << "count " << i << endl;
}
void searchBlackBook(string contacts[], string foName, string loName, ifstream& aInFile) {
cout << "SEARCH FOR A CONTACT\n";
cout << "Type the contacts first name\n:";
cin >> foName;
cout << "Type the contacts last name\n";
cin >> loName;
aInFile.open(OUTPUT_FILE);
checkFileOpen(aInFile);
bool foundName = false;
int nameIndex;
for (int j = 0; j < 4 && !foundName; j++){
bool foundFirstName = false;
bool foundLastName = false;
aInFile >> contacts[j];
if (contacts[j].find (foName) != string::npos){
foundFirstName = true;
cout << "found first name in contacts[i] =" << foName << endl;
}
if (contacts[j].find (loName) != string::npos){
foundLastName = true;
cout << "found last name in contacts[i] = " << loName << endl;
}
if(foundFirstName ==true && foundLastName == true){
foundName = true;
nameIndex = j;
}
cout << "counter = " << j << endl;
cout << contacts[j] << endl; // j gets incremented each time it reads lastName, hten when it reads firstName, hten when it reads phone
}
}
/**
void outputBlackBook(string contacts[], string firstName[], string lastName[], string phonenumber[], ifstream& aInFile, int& i){
aInFile.open(OUTPUT_FILE);
checkFileOpen(aInFile);
for(int i = 0; i < NUMBER; i ++){
aInFile >> contacts[i], firstName[i] >> lastName[i] >> phonenumber[i];
//contacts[i];
sortContacts(contacts, NUMBER);
cout <<contacts[i];
cout << lastName[i] << "\t" << firstName[i] << "\t" << phonenumber[i] << endl;
}
}*/
void checkFileOpen(ifstream& aInFile){
if(aInFile.fail()) {
cout << "input file opened failed\n";
exit(1);
}
}
void checkFileOpen(ofstream& aOutFile){
if(aOutFile.fail()) {
cout << "output file opened failed\n";
exit(1);
}
}
/**
void sortContacts(string contacts[], int NUMBER){
cout << "sorting\n";
int smallest;
for(int i = 0; i < (NUMBER-1); i++){
smallest = i;
for(int j = i+1; j < NUMBER; j++){
int contacts1[j] = contacts[j];
int contacts2[smallest] = contacts[smallest];
if (contacts1[j] < contacts2[smallest]){
smallest = j;
}
}
swap(contacts1[i], contacts2[smallest]);
}
}
void swap(int& aElement1, int& aElement2){
int temp = aElement1;
aElement1 = aElement2;
aElement2 = temp;
}*/This is my code so far.
but search function is not working.
kindly look at the counter problem which i mentioned in previous post
Last edited by code12; Mar 31st, 2008 at 12:59 am. Reason: added information
No there is no input file for the add function. the file ot1.txt is created in add function and all the added contacts are written on this file, later on this file is declared input file from where the contacts will be extracted,this is done in search function, output function(not completed yet)
the ot1.txt file after I added contacts in it is as follows:
barn sam 12
joe mark 123
ogden ken 1234
anderson pam 12345
if u need anything else please let me know.
the ot1.txt file after I added contacts in it is as follows:
barn sam 12
joe mark 123
ogden ken 1234
anderson pam 12345
if u need anything else please let me know.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
O.K. Here's your function:
A few comments. One, line 12, don't hard-code 4 into this loop. It was there in my example because I knew there were 4 records. You don't know how many records there are going to be, so you can't put that number in the code since it's going to change.
Because of this, don't create a for-loop, create a while loop since you don't know how many times you are going through the loop. You have a few choices of how to design this while loop and how to read in the data.
Line 16. As you discovered, this won't work like you want. You have three pieces of information in each line. The >> operator will only grab one of them each pass through the loop. You have a few options. I think the easiest option is as follows: Read from aInFile three times per loop. Keep the j counter but increment it inside the while loop.
I would strongly consider using a struct instead of storing all three pieces of data inside a single string. Something like this:
I think I suggested that before. I don't know if you are familiar with structs, but this one calls for one and it'll simplify things. No more "find" routines. If you decide to use a string still, it can certainly be done, but I would read the data into three separate strings (lastName, firstName, phoneNum). Compare the names entered by the user to these strings using the == operator or the compare function from the string library. Don't use "find". You can then concatenate the three strings into one like this:
Line 1. Why are you passing foName and loName to this function?
Finally, to make sure you don't run into memory problems, at least for now, delete the output file before running this program each time. I believe that it may be adding new entries onto the end of the file each time it runs, so it'll soon get past 4 and could give you problems.
C++ Syntax (Toggle Plain Text)
void searchBlackBook(string contacts[], string foName, string loName, ifstream& aInFile) { cout << "SEARCH FOR A CONTACT\n"; cout << "Type the contacts first name\n:"; cin >> foName; cout << "Type the contacts last name\n"; cin >> loName; aInFile.open(OUTPUT_FILE); checkFileOpen(aInFile); bool foundName = false; int nameIndex; for (int j = 0; j < 4 && !foundName; j++) { bool foundFirstName = false; bool foundLastName = false; aInFile >> contacts[j]; if (contacts[j].find (foName) != string::npos) { foundFirstName = true; cout << "found first name in contacts[i] =" << foName << endl; } if (contacts[j].find (loName) != string::npos) { foundLastName = true; cout << "found last name in contacts[i] = " << loName << endl; } if(foundFirstName ==true && foundLastName == true){ foundName = true; nameIndex = j; } cout << "counter = " << j << endl; cout << contacts[j] << endl; // j gets incremented each time it reads lastName, hten when it reads firstName, hten when it reads phone } }
Because of this, don't create a for-loop, create a while loop since you don't know how many times you are going through the loop. You have a few choices of how to design this while loop and how to read in the data.
Line 16. As you discovered, this won't work like you want. You have three pieces of information in each line. The >> operator will only grab one of them each pass through the loop. You have a few options. I think the easiest option is as follows: Read from aInFile three times per loop. Keep the j counter but increment it inside the while loop.
I would strongly consider using a struct instead of storing all three pieces of data inside a single string. Something like this:
C++ Syntax (Toggle Plain Text)
struct person { string lastName; string firstName; string phoneNum; };
C++ Syntax (Toggle Plain Text)
contacts[j] = lastName + " " + firstName + " " + phoneNum;
Line 1. Why are you passing foName and loName to this function?
Finally, to make sure you don't run into memory problems, at least for now, delete the output file before running this program each time. I believe that it may be adding new entries onto the end of the file each time it runs, so it'll soon get past 4 and could give you problems.
I'm trying this now :
but stil have problems with getting output.
C++ Syntax (Toggle Plain Text)
void searchBlackBook(string contacts[], string foName, string loName, ifstream& aInFile) { cout << "SEARCH FOR A CONTACT\n"; cout << "Type the contacts first name\n:"; cin >> foName; cout << "Type the contacts last name\n"; cin >> loName; aInFile.open(OUTPUT_FILE); checkFileOpen(aInFile); int k = 0; while(!aInFile.eof()){ getline(aInFile, contacts[k]); //cin.ignore(); //cout << contacts[k]; (foName == contacts[k]){ cout <<"foname"<< contacts[k] << endl; } aInFile >> contacts[k]; if (loName == contacts[k]){ cout << "lastnemr" << contacts[k] << endl; } k++; } }
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
C++ Syntax (Toggle Plain Text)
int k = 0; while(!aInFile.eof()){ getline(aInFile, contacts[k]); //cin.ignore(); //cout << contacts[k]; (foName == contacts[k]){ cout <<"foname"<< contacts[k] << endl; } aInFile >> contacts[k]; if (loName == contacts[k]){ cout << "lastnemr" << contacts[k] << endl; } k++; }
Lines 3 and 9: You're getting the entire line of data in line 3, then the last name of the NEXT line of data in line 9 and storing them both in contacts[k] (where k is the same in both instances). You are mixing data lines. Either use a getline statement and don't us the >> operator or use the >> operator three times with no getline statement, but be extremely careful about mixing the two. Either way is workable, as is a mix of the two, but not the way you have it.
If you use the >> operator, don't read it into contacts[k], because contacts[k] represents all 3 pieces of data (last, first, phone) and the >> operator, regardless of how you do it, will only extract one.
Thus in lines 6 and 10 you are comparing oranges and apples. You are comparing a string that contains only the first or last name to a string that contains first, last, and phone number. Do what I suggested in the last post. Read in the data into three separate string variables using the >> operator three times, do the comparisons, than create contacts[k] by "adding"/concatenating the three string variables.
![]() |
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 |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






