I wonder something about finding a specific character in a string.
What I am trying is to find "*" and if that character is found in the string, I will
write "Found" to a file.
When running the code below. All this is happening but the same thing is happening
if I change the string1 to "Number1 - Number2".
So I wonder if I could have missed anything here ?

ofstream File1;
File1.open("C:\\File1.txt");

std::string string1 = "Number1 * Number2";

if( string1.find("*") )
{
    File1 << "Found";
}

You have to check against std::string::npos the return value of the find() method.

std::string::size_type pos = string1.find("*");

if(std::string::npos != pos)
{
    // found a match at 'pos'
}
else
{
    // not found
}

For information on std::string::npos see
http://www.cplusplus.com/reference/string/string/npos.html

this is because:
Return Value for string.find(...) is:
1) The position of the first occurrence in the string of the searched content.
or
2) If the content is not found, the member value npos is returned.

so your if(...) always evaluates to true.

Yes this was the problem. Thank you,
I will experiment with this.

this is because:
Return Value for string.find(...) is:
1) The position of the first occurrence in the string of the searched content.
or
2) If the content is not found, the member value npos is returned.

so your if(...) always evaluates to true.

Added post by mistake, sorry.

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.