Sign in to follow this  
neilski_2003

testing for existance of a character

Recommended Posts

hi guys i am trying to check a word to see if it has a particular character in it and this is what i ahev come up with
[source lang  = "cpp"]

vector<string> ascdsc (vector<string> & words)
{
	string::size_type j = 0;
	string::size_type i = 0;

	for (j = 0; j != words.size(); ++j)
	{
		cout<<words[j]<<endl;
		for (i = 0; i != words[j].size(); ++i)
		{
			cout<<(words[j])[i]<<endl;
			if ((words[j])[i] = "b" && "d" && "f" && "h" && "k" && "l" && "t" && "g" 
				&& "j" && "p" && "q" && "y")
			{
				cout<<"Word has a asc/dsc in it"<<endl;
			}
			else
			{
				cout<<"you don't smell"<<endl;
			}
		}
	}

	return words;
}


only problem is - doing it this way reckons that every character is one of those i have entered. Can anyone tell me what i am doing wrong. I have tried putting == in the if statement but that just creates an error. Any help would be appreciated. Neil

Share this post


Link to post
Share on other sites
To be brief, that doesn't do what you think it does.

&& works for truth (true, false).
= is assignment, not equality.

C++ can convert values to statements, 0 being false, not 0 being true, thus your if is:


if( true and true and true and true and true...

Share this post


Link to post
Share on other sites
You also might want to use the logical OR which is || instead of AND which is && if your just checking to see if the word has atleast one of those letters.

Share this post


Link to post
Share on other sites
The most elegant way to solve this problem is to use the functionality someone else already implemented (as it is always :) )

There is a find_first_of method of the class std::string which suits this problem just fine. I recommend you read up on the documentation yourself to find out why and how, because it's way more correct than I am most of the time.

Hope that helps

Share this post


Link to post
Share on other sites
Slightly off topic:

Hmmm, looking at that code again, how come the compiler compiles what is written there? Aren't the logical operators only working on arguments of type bool and numerical types (unless they're overloaded)? I wouldn't expect them to work on null-terminated strings... Could someone enlighten me just shortly?

Share this post


Link to post
Share on other sites
Quote:
if ((words[j])[i] = "b" && "d" && "f" && "h" && "k" && "l" && "t" && "g"
&& "j" && "p" && "q" && "y")


That's completely wrong.

"b", "d" and so on are pointers. They are non-zero, therefore always true. Chaining "b" && "d" && "f" is meaningless because of that, pretty much as if you had written true && true && true && true ....

It definitely does not check words[j][i] against each of them in turn, if only because = is an assignment and not an equality test, which is written ==.

That line should probably likely read
if ( words[j][i] == 'b' || words[j][i] == 'd' ||
words[j][i] == 'f' || words[j][i] == 'h' ||
words[j][i] == 'k' || words[j][i] == 'l' ||
words[j][i] == 't' || words[j][i] == 'g' ||
words[j][i] == 'j' || words[j][i] == 'p' ||
words[j][i] == 'q' || words[j][i] == 'y' )


Or, better
if (words[j].find_first_of("bdfhkltgjpqy") != std::string::npos)

which removes removes the need for the inner i loop.

Share this post


Link to post
Share on other sites
well that answered my question (before i asked it).

Sorry guess never seen that find_first_off stuff - possibly why i am in the beginners forum.

So i'll stick without it for now until such times as it comes up in the book or i have time to find out about it.

Cheers.

Neil

Share this post


Link to post
Share on other sites
Quote:

So i'll stick without it for now until such times as it comes up in the book or i have time to find out about it.


Now I really don't want to force you into anything :), but I think it's not the best idea to get to know things only once they hit your face up front. Especially if we're talking about methods that are a good solution to our problem at hand. It kinda goes against the correct philosophy of learning new things whenever you have the opportunity. Just my two cents anyway.

And learning to use the language reference is one of the most fundamental things to learn if you're programming. I recommend the MSDN library which is nicely integrated in Visual Studio (if you use VS, that is), and can really speed up the learning process.

P.S. Thanks Fruny for clearing that up. I forgot those were pointers :)

Share this post


Link to post
Share on other sites
I agree with you as an ideal situation. And its not that i won't learn this - everything i do in c++ is documented on paper as well as the practical exercises and I have a page dedicated to new ideas i must revisit.

Its just that my knowledge is such that only entirely structured learning - e.g folowing through this book - is going to give me a sound enough base from which to develop. After all its no use having advanced ideas on how to solve one thing if you have no ideas on how to solve another.

Once this book has finished I will be straight back through the ideas on my list and trying to apply them to the relevant exercises etc, and hopefully then i will have a better understanding of why i've done it and how it works - not just because someone told me to do it on a forum.

This works best for me - and although it might be roundabout it tends to get me results in the long run.

Cheers for all your input.

Neil

Share this post


Link to post
Share on other sites
Hm, very interesting way of learning you got there :) I know it wouldn't work for me because I would be lazy to get back to those things, but if it works for you, it's certainly the right way to do it.

Anyways, have fun!

Morrandir

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this