How do I use "if" statements with strings?

Jul 3, 2013 at 5:27am
Hi, I've got a very simple but annoying problem. I've used google, I've searched other C++ forums, but I haven't found a solution.

1
2
3
if (letter3=="\n"){
letter3==letter;
PrintCharacterLineEnd();


This is a code. the string "letter3" contains the string "\n". What I want to happen is when, letter3 contains the text "\n" I want to go to the function PrintCharacterLineEnd.

However, as of right now it's not working.
Jul 3, 2013 at 5:28am
closed account (Dy7SLyTq)
are you using std::string in <string>? and also its letter3 = letter not ==
Jul 3, 2013 at 7:40am
I'm not sure.

But when I try to use just one equal sign, it won't compile.
Jul 3, 2013 at 7:58am
But when I try to use just one equal sign, it won't compile.


One '=' is an assignment operator and two "==" is the equality operator.

HTH
Jul 3, 2013 at 8:03am
Is this what you are trying to do:

1
2
3
4
5
6
              std::string letter("some random string"), letter3("this is a string \n");
	if (letter3.find("\n") != 0)
	{
		letter3 = letter;
		PrintCharacterLineEnd();
	}
Last edited on Jul 3, 2013 at 8:07am
Jul 3, 2013 at 8:57am
some compilers return different value from
 
letter3.find("\n")

if letter3 don't contain "\n" (eg. some 4294967295 like borland 5 or some -1 ) & we use it directly in 'if()'
so I suggest this :
1
2
3
4
5
6
7
8
int test = -1; // -1 means not containing & othetwise test is locaition of '\n' in our string
std::string letter, letter3 ;
//some actions on strings
test = letter3.find("\n");
if ( test >= 0 ) { //or test != -1	
	letter3 = letter;
	PrintCharacterLineEnd();
}
Jul 3, 2013 at 9:42am
Well mainly "\n" is already in letter3. However even though letter 3=="\n" is true, it won't work.

I also tried "letter3.compare("\n")==true" which kind of works.

Problem with that is that it's always true even with letter3!="\n".
Jul 3, 2013 at 10:03am
You haven't told us what types letter3 and letter are. std::string? Windows CString? C-style char* arrays? Some context would be helpful.
Jul 3, 2013 at 10:08am
I'm not sure.

But when I try to use just one equal sign, it won't compile.

(About using std::string?)

Do you have a more complete version of your code?
Are you including <string> and then using namespace str; ?
???

@ ajh32 / mokhi64

Did you mean

if (letter3.find("\n") != letter3.npos) // or string::npos

As

Return Value
The position of the first character of the first match.
If no matches were found, the function returns string::npos.

From http://www.cplusplus.com/reference/string/string/find/

As string:find returns an unsigned integer value, the value will always be greater than or equal to zero.

And while I'm here...

string::compare returns an int -- you should not be comparing it against bool true or false. You usually compare it against 0 as the method returns:
* -1 if the lhs is lexically of a lesser value than the rhs
* 0 if they're equal
*1 if the lhs is less than the rhs

(just like the strcmp function.)

Just like it explains here:
http://www.cplusplus.com/reference/string/string/compare/

Andy
Last edited on Jul 3, 2013 at 10:24am
Jul 3, 2013 at 2:51pm
closed account (Dy7SLyTq)
post all of your code. it will help
Jul 3, 2013 at 3:38pm
I think letter is a char and letter3 is a string so that is why he can't assign the string to the char. Maybe be he meant letter3[3] = letter; but I could be very well wrong.
Jul 3, 2013 at 5:18pm
It looks like you are using character arrays. In this case you have to write


if ( std::strcmp( letter3, "\n" ) == 0 ) // In case of equality

or

if ( std::strstr( letter3, "\n" ) ) // in case of containing
Jul 6, 2013 at 7:28am
The string.find thing didn't seem to work.

However, this code goes to the proper place when "\n" is in letter3.

[code]if (letter3.compare("\n")==1){
cout<<"You're here"<<endl;
PrintCharacterLineEnd();
main();
cin.get();}[/quote]

letter3 is a string containing "\n".

But it's true every time, even when it should be false.

I only want this code executed when "letter3=="\n".
Jul 6, 2013 at 8:42am
It might help to look at the documentation for the methods you are using.

http://www.cplusplus.com/reference/string/string/compare/
Jul 6, 2013 at 9:49am
It might also help if you showed us the whole code so that we can see what types you're actually using, rather than leaving us to guess.

Also, you're calling main() from within your code. That's not legal in C++.
Topic archived. No new replies allowed.