I need to be able to display whether or not a person searched for "is a friend" if found in the file and "isn't a friend" if not found in the file. I also need to continue processing names until "END" is typed in. Any help would be appreciated!

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

char name;
void bubbleSort (string names[], int size=0);
int binarySearch(string names[], int size, string value);
void displayit (string names[], int count);

int main ()

    ifstream infile;
    string names[200], valu, arrayLength;
    int count=0, size=0;

    if (!infile)
        cout<< "Could not open myfriends file"<<endl;
        while (getline (infile, names[count]))
        displayit ( names,  count);
        bubbleSort(names, size);
        cout<< "Please enter a name or END to terminate: "<<endl;
        getline (cin, names[count]);

    cout<< names[count]<< " is my friend"<<endl;

    infile.close ();

system ("pause");
return 0;

void displayit (string names[], int count)
    for (int i=0; i<count; i++)
            cout<< names[i]<<endl;


void bubbleSort (string names[], int size)
    bool swap;
    string temp;

        for (int count=0; count <(size-1); count++)
            if (names[count]> names[count+1])
    } while (swap);

 int binarySearch(string names[], int size, string value)
     int first = 0,last = size - 1, middle, position = -1;
     bool found = false;

      while (!found && first <= last)   
          middle = (first + last) / 2;    

          if (names[middle] == value)      
              found = true;         
              position = middle;

          else if (names[middle] > value)  
              last = middle - 1;      
              first = middle + 1;   


      return position;


Recommended Answers

All 2 Replies

It would help if you would ask a specific question. What happens when you try to run your program? Where do you think the problem lies?

Just few quick observations:
line 24 ought to also include a test that count has not exceeded the array size
line 27 ought to use count instead of size
line 29 what string do you really mean to use here?
line 30 you should store the result from the binary search, use that to determine friend or not

One thing I see is you're adding the input name directly to the array , before you search the array for the name.

I would suggest using a simple menu, with the options Find a friend, add a friend, remove a friend, display friends, and end the program.

Be a part of the DaniWeb community

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