Hey, i am trying to search an array of surnames. I am entering in a surname, and if that surname is in the array, i want to to bring out all other details for that surname that are stored in a structure. At the moment, i have this working for the reference number. The code for that is:

cout << "Please enter an associate reference number :" << endl;
cin >> SearchNum;
 
while((RecNum < MAX_QUOTES) && (SearchNum != Quote[RecNum].RefNumber))
 
{
RecNum++;
}

This then, if found, out puts all the details in the Quote structure for all the details of that record number.

However, when i try this for the surname, which is very similar, it doesn't work:

cout << "\n\nPlease enter an associate surname :" << endl;
cin.getline(SearchName,NAME_LEN);
 
while((RecNum < MAX_QUOTES) && (SearchName != Quote[RecNum].Surname))
 
{
RecNum++;
}

SearchName takes in the surname i enter along with some other stuff after it. I was wondering how to get rid of all the stuff after the surname to get this to work. I think its something to do with '/0' but that just stores up the remaining space with /0. It wont bring out the results for Snape as searchname is stored as Snape/0/0/0/0.

Any help would be great.

Ian

It seems that you have declared searchnum as a C style string or a null terminated string. If you want to compare C style strings, you can't do it using the not equal to operator. You need to use the function [search]strcmp[/search] function which returns 0 when the two strings passed are equal.

Better yet, use C++ string class, accept the string using [search]getline[/search] and then use the != operator which is overloaded for the sake of effortless string comparision.

The string returned by getline probably has a newline on the end of it (which won't be in any string you're comparing against).

Or it's the classic mixing "cin >> var" with getline, and getline just returns with the newline which cin left behind.

I knew i had to use strcmp. Just needed verification of this. Will give this a go now and report back. Thanks for your quick replies and help guys!

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.