I'm trying to use a while loop to search an array, using a users search input, at the minute I have listed one word film titles into a standard text file, and then the while loop keeps searching the file until it is found, and then it is output on the console.. But I have the problem that the operator "==" can't be applied to string variables? How would I fix this? Thanks a lot! And merry Christmas, here Is my code:
//Declare variables
int iOneWordTitle= 0;
string sSearch;
//Declare array
const int iFilm = 7;
string[] sOneWordTitle = new string[iFilm];
//Add heading to console
Console.WriteLine("List of one word film titles");
Console.WriteLine();
//ask user what they want to search for
Console.WriteLine("What film would you like to search for?");
sSearch = Console.ReadLine();
//Read the film names from the datafile
using (StreamReader sr = new StreamReader("filmnames.txt"))
{
while (iOneWordTitle < iFilm)
{
sOneWordTitle[iOneWordTitle] = (sr.ReadLine());
iOneWordTitle++;
if (sSearch == sOneWordTitle)
{
Console.WriteLine(sSearch + " was found at position " + iOneWordTitle);
}
else
{
Console.WriteLine("The film was not found");
}
}
}
if (sSearch == sOneWordTitle)compares a string to a string[]. You may want to figure out what you really want to compare there.