I'm having trouble writing a function that searches through an array of structures for a string value.

The structure looks like this:

struct Computer{
   string Model;
   string Brand;
   int Price;
};

For some reason I have no problem finding the Price values, but the function that is supposed to compare a string to another string it always defaults to "not found" which is supposed to happen if the Model does not exist in the database. I checked to see if the array does have all the data, and it does.

Here is what I have

void FindModel( ifstream& Process, ofstream& Log, Computer Database[], Computer Temp, int NumItems )
{
	int Position;

	// Get rest of line
	getline( Process, Temp.Model, '\n' );

	Log << left
		<< setw(30) << "Looking for Computer named:"
		<< setw(38) << Temp.Model
		<< endl;

	Position = ModelSearch( Database, Temp, NumItems );

	if ( Position = -1 )
	{
		Log << Temp.Model << " not found" << endl;
		Log << "--------------------------------------------------------------------" << endl;
	}
	else
	{
		Log << right
			<< setw(3)	<< Position << ":  "
			<< left
			<< setw(26) << Database[Position].Model
			<< setw(20) << Database[Position].Brand
			<< setw(10) << Database[Position].Price 
			<< endl;
	}
}

The ModelSearch function:

int ModelSearch( const Computer Database[], Computer& Temp, int NumItems )
{
	int Position = 0;

	while ( ( Position < NumItems ) && ( Database[Position].Model != Temp.Model ) )
		Position++;

	if ( Position < NumItems )
		return Position;
	else
		return MISSING;
}

The function is doing a case-sensitive search -- maybe the search string is not the same case as the strings in the array. You might to a case insensitive compare by converting both strings to all upper case (or lower case) before performing the comparison. Otherwise I see no reason for the code you posted to fail.

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.