Searching array problems

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Searching array problems

 
0
  #1
Feb 12th, 2007
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
Last edited by iaaan; Feb 12th, 2007 at 2:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Searching array problems

 
0
  #2
Feb 12th, 2007
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.
Last edited by ~s.o.s~; Feb 12th, 2007 at 2:11 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Searching array problems

 
0
  #3
Feb 12th, 2007
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.
Last edited by Salem; Feb 12th, 2007 at 2:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Re: Searching array problems

 
0
  #4
Feb 12th, 2007
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1162 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC