•
•
•
•
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
![]() |
•
•
Join Date: Jan 2007
Posts: 16
Reputation:
Rep Power: 2
Solved Threads: 0
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:
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:
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
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.
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.
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.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
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.
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- using boolean expression on an array (Java)
- Problem installing XP on a SATA Striping Array (Storage)
- error 88:'(' expected when trying to display an array any help (Pascal and Delphi)
- Conver int Array into a String (Java)
Other Threads in the C++ Forum
- Previous Thread: urgently needed
- Next Thread: C++ const help



Linear Mode