So i try to write a linear search algorithm that allow the user to enter a full or partial player name, and it have to be case insensitive. If the player name is found, the program will display the full name of the player, their number, and their point per game. This is what I have so far:
//*********************************************************************
const int NUM_PLAYERS = 3;
const int NAME_SIZE = 31;
void read_Data(char names[][NAME_SIZE], int numbers[], float ppg[], int num_players);
int lin_Search(char names[][NAME_SIZE], int number[], float ppg[], int num_players);
int main()
{
system("Color 0A");
char names [NUM_PLAYERS][NAME_SIZE];
int numbers [NUM_PLAYERS];
float ppg [NUM_PLAYERS];
void read_Data(char names[][NAME_SIZE], int numbers[], float ppg[], int num_players)
{
// Open file and check for error
ifstream inFile;
inFile.open("players.txt");
if(!inFile)
{
cout << "Error opening points.txt!\n";
cout << "Press Enter to exit!";
cin.get(); // Pause for the message
exit(102); // include cstdlib
}
int i = 0;
while(i < NUM_PLAYERS && !inFile.getline(names[i], NAME_SIZE).eof())
{
inFile >> numbers[i];
inFile >> ppg[i];
inFile.ignore();
// validate input
i++;
}
for (int j = 0; j < i; j++)
{
cout << names[j] << " " << numbers[j] << " " << ppg[j] << endl;
}
inFile.close();
}
int lin_Search(char names[][NAME_SIZE], int numbers[], float ppg[], int num_players)
{
char lookUp[NAME_SIZE]; // To hold user's input
char *strPtr = NULL; // To point to the found player
int index; // Loop counter
bool found = false;
// Prompt the user for a player number.
cout << "Enter a player name to search for: ";
cin.getline(lookUp, NAME_SIZE);
// Search the array for a matching substring
for (index = 0; index < NUM_PLAYERS; index++)
{
strPtr = strstr(names[index], lookUp);
if(strPtr != NULL)
found = true;
}
// If a matching substring was found, display the player info.
if (found)
{
found = false;
cout << names[index] << endl;
cout << numbers[index] << endl;
cout << ppg[index] << endl;
}
else
cout << "No matching player was found. \n";
return -1;
}
So went i try search for the player name, it displayed weird and garbage number. Can someone tell me what I did wrong?
Ok,
Do you know what happens when you try to pass an array to a function?
What you get in the function is a pointer to the first element of the array that was passed. You were hitting random elements of memory and that is why you were getting the garbage values. I would bet anything that the compiler flags that as a warning.
Yous should write the following in int lin_Search(char names[][NAME_SIZE], int numbers[], float ppg[], int num_players):
Make an array in the function and use a for loop to assign the values of names[][] to the new array one by one. You have to use pointer arithmetic because you only have a pointer to the first element.
You would do it like this:
1 2 3 4 5
char ar[][NAME_SIZE];
for(int i = 0; i < NAME_SIZE; i++)
{
ar[][i] = *(names + i);
}
Hi, thank you for the reply. I have fixed the problem with the search function but i don't know how to make it case insensitive. Can you help me walkthrough it?