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>
#include<sstream>
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;

    infile.open("myFriends.dat");
    if (!infile)
        cout<< "Could not open myfriends file"<<endl;
    else
    {
        while (getline (infile, names[count]))
        {count++;}
        displayit ( names,  count);
        bubbleSort(names, size);
        cout<< "Please enter a name or END to terminate: "<<endl;
        getline (cin, names[count]);
        binarySearch(names,size,valu);
    }

    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;

    do
    {
        swap=false;
        for (int count=0; count <(size-1); count++)
        {
            if (names[count]> names[count+1])
            {
                temp=names[count];
                names[count]=names[count+1];
                names[count+1]=temp;
                swap=true;
            }
        }
    } 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;      
          else         
              first = middle + 1;   

      }  

      return position;
 }

 

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.

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.