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:

#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() {

}

*/

>>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)!

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:

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);

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;

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

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);

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.

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.

please can you help how to make it accept more than one character

See the example I posted in post #4 of this thread.

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

as suggested before you don't need that for loop. Just ask the three questions

cout << "Type the contacts first name:\n";
cin >> firstName;
cout <<"Type the contacts last name:\n";
cin >> lastName;
cout <<"Type the phonenumber\n";
cin >> phonenumber;
//
// You also need to separate the names with a space
outFile << firstName << " " <<  lastName << " " << phonenumber << "\n";
selectionMsg(choice);

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

#include<iostream>
#include<cstring>
#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, int& choice);
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;
        introMsg();
        selectionMsg(choice);
        if((choice = 1)){
                addBlackBook(firstName, lastName, phonenumber, outFile, choice);
        }/**else if(choice == 2){
                deleteFromBlackBook();
        } else if (choice == 3){
                searchBlackBook(firstName, lastName);
        } else if (choice == 4){
//              inFile.open("abc12.txt");
                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, int& choice){

//                for(int j = 0; j < 3; j++ ){
                 //               if(j==0){
                                cout << "Type the contacts first name:\n";
                                cin >> firstName;
                           //     }else if(j==1){
                                cout <<"Type the contacts last name:\n";
                                cin >> lastName;
                             //   }else{
                                cout <<"Type the phonenumber\n";
                                cin >> phonenumber;
                               // }
                                //cout << j;
      //          }
                cout << firstName<<" " << lastName << " " <<phonenumber << endl;
                outFile << firstName<<" " << lastName<<" " << phonenumber<< endl;
                selectionMsg(choice);
}

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() {

}

*/

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){

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:

void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){

                        cout << "Type the contacts first name:\n";
                        for (int i=0; i< NUMBER; i++){
                                cin >> firstName[i];
                        }
                        cout <<"Type the contacts last name:\n";
                        for(int i = 0; i < NUMBER; i++){
                                cin >> lastName[i];
                        }
                        cout <<"Type the phonenumber\n";
                        for(int i = 0; i < NUMBER; i++){
                                cin >> phonenumber[i];
                        }
                cout << firstName[i] << lastName[i] << phonenumber[i] << endl;
                outFile << firstName[i] << lastName[i] << phonenumber[i] << endl;
                selectionMsg(choice);

above in main i have used it like this:

#include<iostream>
#include<cstring>
#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, int& choice);
void checkFileOpen(ofstream& aoutFile);
//char outputBlackBook();
//void deleteFromBlackBook();
//char searchBlackBook();
const int NUMBER = 20;
int main(){
   ifstream inFile;
        ofstream outFile;
        outFile.open(OUTPUT_FILE, ios::app);
        char firstName[NUMBER];
        char lastName[NUMBER];
        char phonenumber[NUMBER];
        int choice;
        introMsg();
        selectionMsg(choice);
        if((choice = 1)){
                addBlackBook(firstName, lastName, phonenumber, outFile, choice);
        }/**else if(choice == 2){
                deleteFromBlackBook();
        } else if (choice == 3){
                searchBlackBook(firstName, lastName);
        } else if (choice == 4){
//              inFile.open("abc12.txt");
                outFile.open("out1.txt");
                outputBlackBook(out);
        }*/

return 0;
}

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:

void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){

                        cout << "Type the contacts first name:\n";
                        for (int i=0; i< NUMBER; i++){
                                cin >> firstName[i];
                        }
                        cout <<"Type the contacts last name:\n";
                        for(int i = 0; i < NUMBER; i++){
                                cin >> lastName[i];
                        }
                        cout <<"Type the phonenumber\n";
                        for(int i = 0; i < NUMBER; i++){
                                cin >> phonenumber[i];
                        }
                cout << firstName[i] << lastName[i] << phonenumber[i] << endl;
                outFile << firstName[i] << lastName[i] << phonenumber[i] << endl;
                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.

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

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.

void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice)
{
     cout << "Type the contacts first name:\n";
     cin >> firstName;

     cout <<"Type the contacts last name:\n";
     cin >> lastName;

     cout <<"Type the phonenumber\n";
     cin >> phonenumber;

     cout << firstName << " " << lastName << " " << phonenumber << endl;
     outFile << firstName << lastName << phonenumber << endl;
     selectionMsg(choice);
}

If this doesn't work, I'm guessing that the problem is in the way you are calling the function.

above in main i have used it like this:

#include<iostream>
#include<cstring>
#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, int& choice);
void checkFileOpen(ofstream& aoutFile);
//char outputBlackBook();
//void deleteFromBlackBook();
//char searchBlackBook();
const int NUMBER = 20;
int main(){
   ifstream inFile;
        ofstream outFile;
        outFile.open(OUTPUT_FILE, ios::app);
        char firstName[NUMBER];
        char lastName[NUMBER];
        char phonenumber[NUMBER];
        int choice;
        introMsg();
        selectionMsg(choice);
        if((choice = 1)){
                addBlackBook(firstName, lastName, phonenumber, outFile, choice);
        }/**else if(choice == 2){
                deleteFromBlackBook();
        } else if (choice == 3){
                searchBlackBook(firstName, lastName);
        } else if (choice == 4){
//              inFile.open("abc12.txt");
                outFile.open("out1.txt");
                outputBlackBook(out);
        }*/

return 0;
}

Function call looks fine except you have a problem here:

if((choice = 1)){
                addBlackBook(firstName, lastName, phonenumber, outFile, choice);
        }

You want == in the if statement, not =.

i haven't got yet how to solve this problem.
I need to enter the first name last name and phonenumber of the person in such a way that later on i can even search that entry using the first and last name.

i haven't got yet how to solve this problem.
I need to enter the first name last name and phonenumber of the person in such a way that later on i can even search that entry using the first and last name.

Well, you have a few choices. Which choice you use depends on how much you know already. First decide whether you want to use C-style strings or C++-style strings. You've included the string library, but I don't think you've used them yet. Second you'll decide whether to use a struct (or class) or not. That will depend on whether you are familiar with structs and/or classes. Regardless, you'll set up one or more array(s) of a struct/class or strings somehow (or vector(s) of structs/classes or strings). Let's assume you have a maximum of 30 people in your phone book. Personally, I would use C++-style strings and a struct if you are familiar with them:

struct person
{
     string firstName;
     string lastName;
     string phoneNumber;  // or could make it an integer
};

const int MAX_NUM_PEOPLE = 30;
void addBlackBook(person people[], int numPeopleInBlackBook);

int main ()
{
     // code
     person people[MAX_NUM_PEOPLE)
     // code
     // call function addBlackBook
     //code
     return 0;
}


void addBlackBook(person people[], int numPeopleInBlackBook)
{
     // Create a new variable of type person
     // ask for information about new person
     // add new person to people array
     // increment numPeopleInBlackBook
}

If you don't use a struct, maybe have three arrays of type string instead of one array of type person. Make these arrays of strings rather than arrays of characters.

string firstName[MAX_NUM_PEOPLE];
string lastName[MAX_NUM_PEOPLE];
string phonenumber[MAX_NUM_PEOPLE];

There are lots of ways to do this project. It depends on what you know how to do and what your personal preference is. One way that won't work if you have more than one person to store is only defining three character arrays of 20 characters each in main, because that's just not going to be enough reserved space to hold multiple people.

start quote:

void addBlackBook(person people[], int numPeopleInBlackBook)
{
     // Create a new variable of type person
     // ask for information about new person
     // add new person to people array
     // increment numPeopleInBlackBook
}

end quote.

Come to think of it, there's actually no reason to create a NEW variable here, then ADD it to the array since the array has already been created. You can just read the new information directly into the array.

I was compiling my code today and ir gets compiled but shows this error instead of giving the requierd results alongwith cout statements.
For the part which is creating problem the code is as follows

void searchBlackBook(string contacts[], string foName, string loName, ifstream& aInFile, int i) {
        int x;
        int y;
        cout << "SEARCH FOR A CONTACT\n";
        cout << "Type the contacts first name\n:";
        cin >> foName;
        cout << "Type the contacts last name\n";
        cin >> loName;
        string l = foName;
        string m = loName;
        aInFile.open(OUTPUT_FILE);
        checkFileOpen(aInFile);
              for(int i = 0; i < NUMBER; i ++){
                        aInFile >> contacts[i];
                        cout << contacts[i];
                        int a = foName.length();
                        cout <<"length of fname=" <<  a << endl;
                x = contacts[i].find(l, a);
                        cout << "occurence of first string =" << x << endl;
                        int b = loName.length();
                        cout << "length of lname" << b << endl;
                y = contacts[i].find(m, b);
                        cout << "occurence of second string =" << y << endl;
               if (foName == contacts[i].substr(x, a) && loName == contacts[i].substr(y, b)){
                               cout << "output"<< contacts[i]  <<   endl;
               }
                }
}

This error is shown :
Type the contacts first name
:11
Type the contacts last name
11
relength of fname=2
occurence of first string =-1
length of lname2
occurence of second string =-1
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Aborted
Please help me what does this mean and how this problem could be solved. Thanks

Thanks, I got it where the problem is, but i'm not able to figure out how can I solve that problem. I used this

x = contacts[i].find(foName);

to figure out where the substr foName is located in the string contacts[NUMBERS]. NUMBERS is the number of contacts which coild be stored in a phonebook.
Now i was triing to do something like this:
I was thinking if the user stored a firstname as donald and last name as frank in the phonebook at location contacts[4], then if later on he trie sto search that contact and he types donald under foName then the loop should search the whole phonebook for this foName.And if not found will output contact not found.
for this to work i used

x = contacts[i].find(foName);

where foName is the variable under which the string foName is stored. by doing this i wanted to find where in the string named contacts[NUMBER] this substr called foNmae is stored. but I'm not able to figure out how could I make this work.
ONe thing more, I know the syntax for find is like this

x = string.find("substr");

under the "substr" I think you actually have to type the string which u want to find . Now i don't know what the user will type under foName, so actually i can't mention the name donald in inverted commas like this

x = contacts[i].find("donald");

I can only give the variabe under which it was stored. I think this even is creating some kind of problem.
Please help me, how can i find where that substr enrered by user is located in the string contacts[NUMBERS]
Thanks

What exactly is this function supposed to do (in English, not code)? Is it supposed to traverse the phone book and see whether a certain person is already in it or not? And then, based on that, if the person IS in the phone book, it is supposed to output the phone number? And if not, do nothing?

Why are you passing i to this function?

void searchBlackBook(string contacts[], string foName, string loName, ifstream& aInFile, int i)

What does i represent and do you ever use this i parameter in your function? If not, don't pass it. If you DO use it, use a different name for it because you have an i inside the function that is used as a counter and it gets confusing.

yes I shouldn't use i parameter in function. I deleted it.
Regarding what my code for search is doing is: It should take foName, loName from the user which he wants to search from the phonebook. then it should find foname in string contacts. if found it will give the loaction otherwise will say not found(i haven't wrriten anything yet for showing that it is not found).
then will search for the loName in contacts.
I think if it find the location of foName and loName in string contactsthen it means that what is typed by user already exists in the string? isn't it.
then i guess there is no need to compare it later on using this

if (foName == contacts[i].substr(x, a) && loName == contacts[i].substr(y, b)){
                   cout << "output"<< contacts[i]  <<   endl;
  }

Well, let's think about this. Let's say you have 4 names in this phone book (number on left is index number of contacts array):

0 Joe    Smith
1 Jim    Dobbs
2 Joe    Johnson
3 Hank   Smith

Let's say the user enters "Hank" and "Smith", which is in the contacts array. It seems to me that you want to know two things. One, is "Hank Smith" in the contacts array? Two, if the answer to question 1 is yes, what is the index of "Hank Smith". So if the user enters "Hank" and "Smith", the answers should be "Yes" and 3, respectively. If the answer to question 1 is no, don't try to answer question 2 since there is no index. So do this (pseudocode):

bool foundName = false; // haven't found name yet;
int nameIndex;

for (int i = 0; i < 4 && !foundName; i++)
{
     bool foundFirstName = false;
     bool foundLastName = false;

     // search contacts[i] for first name.  If present,
     // set foundFirstName to true;
     // search contacts[i] for last name.  If present,
     // set foundLastName to true;

     // if foundFirstName and foundLastName are both
     // true, set foundName to true and set nameIndex
     // to i.
}

By the end of this code, you know whether "Hank Smith" is in the phone book, and if so, you know which element of contacts contains that name. Now go from there.

Thanks. I'm going to try this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.