0

one more thing I was not able to use the

string.find("substr")

what should I use to find the firstName from the string contacts
and then to find lastName in the same string contacts

0

Don't use the word "string". Use the variable name (in your case, contacts):


#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string firstName = "Hank";
    string name = "Hank Smith";
    
    if (name.find (firstName) != string::npos)
         cout << firstName << " is in this string: " << name << endl;
    else
         cout << firstName << " is not in this string: " << name << endl;

    return 0;
}

See this link too:
http://www.cplusplus.com/reference/string/string/find.html

And this one: npos basically just means -1 or "not found".
http://www.cplusplus.com/reference/string/string/npos.html

0

that was my ques before that the user is going to type the firstName and lastName, which he wants to search in phonebook. In taht case I don't know what string firstName & string lastName is going to type so i can't do:

string firstName = "Hank";
string name = " Hank Smith ";

I only know the varaible name which is firstName & lastName

so I can't do like this even

string firstName 
string contacts[NUMBERS];
for(int j= 0; j< NUMBER; j++){
if (contacts[j].find (firstName) != string::npos)
         cout << firstName << " is in this string: " << name << endl;
    else
         cout << firstName << " is not in this string: " << contacts[j] << endl;
}

What should I do?

0

that was my ques before that the user is going to type the firstName and lastName, which he wants to search in phonebook. In taht case I don't know what string firstName & string lastName is going to type so i can't do:

string firstName = "Hank";
string name = " Hank Smith ";

I only know the varaible name which is firstName & lastName

so I can't do like this even

string firstName 
string contacts[NUMBERS];
for(int j= 0; j< NUMBER; j++){
if (contacts[j].find (firstName) != string::npos)
         cout << firstName << " is in this string: " << name << endl;
    else
         cout << firstName << " is not in this string: " << contacts[j] << endl;
}

What should I do?

Doesn't matter that you don't know the names ahead of time. This works equally well:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string firstName;
    string name;
    cout << "Enter first name:";
    cin >> firstName;
    cout << "Enter full name:";
    cin >> name;
    
    if (name.find (firstName) != string::npos)
         cout << firstName << " is in this string: " << name << endl;
    else
         cout << firstName << " is not in this string: " << name << endl;

    return 0;
}

Go back to your original code. You're calling first name foName and last name loName. You already have the contacts array declared, so so get rid of this line:

string contacts[NUMBERS];

You changed your loop variable from i to j, so just do this:

if (contacts[j].find (foName) != string::npos)
{
     // found first name in contacts[j]
}

if (contacts[j].find (loName) != string::npos)
{
     // found last name in contacts[j]
}
0

I tried but it's not giving me the names after searching... Please if u can help...shd i show the code how i have written??

0

Post the code for the whole function as well as what input you are using, what the resulting output is, any compiler/runtime errors, and the contents of the contacts array that is passed to the function.

0
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[j] =" << foName << endl;
                        }
                        if (contacts[j].find (loName) != string::npos){
                                foundLastName = true;
                                cout << "found last name in contacts[j] = " <<  loName << endl;
                        }
                        if(!foundFirstName && !foundLastName){
                                foundName = true;
                                nameIndex = j;
                        }
                        cout << "counter = " << j << endl;
                        cout <<  contacts[j] << endl;
        cout <<  contacts[j] << endl;
                }
}

code of function search is like this. If I have five contacts stored in a file like
barn sam 12
joe mark 123
ogden ken 1234
anderson pam 12345

i doesnot show anything if i search for any contact stored at positon 1,2, 3.
only can it works for position 0, which is barn sam 12
barn as last name, sam as first name.
it is stored in this way in phonebook: lastName/tfirstname/tphonenumber/n

though it works but the ouput is like this
SEARCH FOR A CONTACT
Type the contacts first name
:sam
Type the contacts last name
barn
found last name in contacts[j] = barn
counter = 0
barn
found first name in contacts[j] =sam
counter = 1
sam
counter = 2
12
What would you like to do
Type 1 to ADD a contact
Type 2 to DELETE a contact
Type 3 to SEARCH for contact
Type 4 to OUTPUT ALL contacts
Type exit to terminate the program
Is this incrementing each time when it reads firstName & lastName

if I search for ken ogden the result is like this:
SEARCH FOR A CONTACT
Type the contacts first name
:ken
Type the contacts last name
ogden
counter = 0
barn
What would you like to do
Type 1 to ADD a contact
Type 2 to DELETE a contact
Type 3 to SEARCH for contact
Type 4 to OUTPUT ALL contacts
Type exit to terminate the program

Should I post the code for whole program or this only will help.

0

I added lines 38 to 42. Look carefully at line 27. You're saying that the name has been found if the first name hasn't been found and the last name hasn't been found.

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[j] =" << foName << endl;
		}
		if (contacts[j].find (loName) != string::npos)
		{
			foundLastName = true;
			cout << "found last name in contacts[j] = " <<  loName << endl;
		}
		if(!foundFirstName && !foundLastName)
		{
			foundName = true;
			nameIndex = j;
		}
		cout << "counter = " << j << endl;
		cout <<  contacts[j] << endl;
		cout <<  contacts[j] << endl;
	}


	if (foundName)
	{
		cout << "This name has been found at index " << nameIndex;
		cout << ": " << foName << " " << loName << endl;
	}
}
0

So i should do like this

if (firstname == true && lastName == true)

on line 27.

and will that proble of counter be solved if i add lines 38 to 43

0

I'm worried about the counter being gettin incremented because in add contact function I have stored lastName/tfirstName/tphone/n
at locations contacts
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.

0

So i should do like this

if (firstname == true && lastName == true)

on line 27.

and will that proble of counter be solved if i add lines 38 to 43

I'm not sure what counter problem you are referring to. Your counter output simply displays j whether the name is found or not. Lines 38 to 42 are after the for loop ends intentionally. They are a test to see whether the name has been found successfully or not.

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.

0

I'm worried about the counter being gettin incremented because in add contact function I have stored lastName/tfirstName/tphone/n
at locations contacts
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.

I think you're going to have to post your entire revised program.

0
#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

0

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.

0

O.K. Here's your function:

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
	}
}

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:

struct person
{
     string lastName;
     string firstName;
     string phoneNum;
};

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:

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.

0

I'm trying this now :

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++;
}
}

but stil have problems with getting output.

0
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++;
}

Line 6 : I assume there is supposed to be an "if" in front of those parentheses?
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.