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

	if ( Position < NumItems )
		return Position;
		return MISSING;
10 Years
Discussion Span
Last Post by Ancient Dragon

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.