How to write a lin_search using C-style string/string class?

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:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <new>
#include <Windows.h>
using namespace std;

//*********************************************************************
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];

read_Data(names, numbers, ppg, NUM_PLAYERS);
lin_Search(names, numbers, ppg, NUM_PLAYERS);

return 0;
}

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

I think that should fix the problem.
Good luck!
Last edited on
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?
Topic archived. No new replies allowed.