Hello,

Can some one give me a hand on my search algorithm? I know it's not working right and am not sure how to make it work properly. It is on line #'s 69 to 73. It has to search for the name that is already stored in the array and return either cout statements provided. Any help would be appreciated.

#include<iostream>
#include<cstring>

using namespace std;
void printArray(char**,int);//Function declaration
char** showNames(char **names,char **names2,int& size, int& size2, int& size3);//Char names declaration
char** addEntry(char **names,char **names2,int& size, int& size2);//Char addEntry declaration
char** deleteEntry(char **names,char **names2,int& size, int& size2);//Char deleteEntry declaration

int main()
{

     int size = 5;//Constant size for size
     int size2 = 20;//Constant size for size2
     int size3 = 1;//Constant size for siz3
     char **names  =  new char*[size];//Creates a double pointer that points to an array of 5 strings
     char **names2 = new char*[size3];
     
     showNames(names,names2,size,size2, size3);//Function call for names  
     cout << "\nOriginal List" << endl;
     printArray(names,size);//Prints out the names to test that they are being inputed correctly
     cout << "\nOne name added to the list" << endl;
     names = addEntry(names,names2,size,size2);//newname is being set to the addentry function
     printArray(names,size);//Prints out the results
     cout << "\nOne name deleted from the list" << endl;
     names = deleteEntry(names,names2,size,size2);//newname is being set to the addentry function
     printArray(names,size);//Prints out the results

return 0;
}

char** showNames(char **names,char **names2,int& size, int& size2, int& size3)
{

     for(char i = 0; i < size; i++)//for loop to iterate across an array
     names[i] = new char[size2];//Creates a dynamic array with the number os size2 elements

     strcpy(names[0], "Jim");//Copies the name Jim into array position 1
     strcpy(names[1], "Joe");//Copies the name Joe into array position 2
     strcpy(names[2], "Kelly");//Copies the name Kelly into array position 3
     strcpy(names[3], "Lisa");//Copies the name Lisa into array position 4
     strcpy(names[4], "Mark");//Copies the name Mark into array position 5
       

     for ( char j = 0; j < size3; j++)
     names2[j] = new char[size2];
     strcpy(names2[0], "Kilroy");//Copies the name Jim into array position 1

}

char** addEntry(char **names,char **names2,int& size, int& size2)//Function definition for char** addentry
{

     char **newarray = new char*[size + 1];//Creates a new double array pointer that points to an array 1 size bigger than 5
     for (int i = 0;i < size; i++)//Used to iterate names over the new array
         newarray[i] = names[i];//Sets newarray equal to the names array
     size++;//Increases the array by one
     newarray[size-1] = new char[size2];//Used to set newarray to a new dynamic array
     strcpy(newarray[size - 1], "Kilroy");//Copies the new name to the end of newarray
     //for (int i = 0; i < size; i++)//Interates the names over newarray
     //cout << newarray[i];//Used to print out the names
     delete [] names;//Deletes the array named names
     return newarray;//Returns newarray
}

char** deleteEntry(char **names,char **names2,int& size, int& size2)//Function definition for char** deleteEntry
{
     for ( int x = 0; x < size; x++)
          if ( names[x] = strcpy(names[x], "Jim"))
               cout << "You Found Him";
          else
               cout << "You Did Not Find Him";

}
void printArray(char **names,int size)//Function definition for printArray
{
     for (int i = 0;i < size;i++)//Used to iterate the names
         cout << names[i] << " ";//Prints out the names that are stored
     cout << endl;
}

Serious error on line #70. You are attempting to perform an assignment operation instead of a boolean comparison. Since you are using cstrings, I would recommend use of the strcmp() function from <cstring>

#include<cstring>

for (int x = 0; x < size; x++)
{
     if (strcmp(names[x], "Jim") == 0)
     {
          cout << "You Found Him";
     }
     else              
     {
           cout << "You Did Not Find Him";
     }
}

Also 'names2' and also the memory allocated in addEntry fn is not being deleted anywhere.

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.