User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,936 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,595 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 832 | Replies: 3
Reply
Join Date: Jan 2007
Posts: 16
Reputation: iaaan is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Searching array problems

  #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 1:01 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,855
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Searching array problems

  #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 strcmp function which returns 0 when the two strings passed are equal.

Better yet, use C++ string class, accept the string using getline 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 1:11 pm.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,638
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 415
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Searching array problems

  #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 1:14 pm.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Jan 2007
Posts: 16
Reputation: iaaan is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Re: Searching array problems

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC