Hi i'm trying to write a program and i've encountered some problem with the if part... Can we use string in if statement? because no matter how i input a string word or a character, it won't prompt to the if statement that i want....
Here is a simple example code for it.Even if i put yes or No it still won't print out "ok!" for me so can explain to me why? or my code wrong in any part?
1) If you're using C and you must use char arrays, you want strcmp. strcmp stands for "string compare" and will compare two C-style strings (char arrays). It will return 0 if the strings are identical:
1 2 3 4 5 6
char choice[] = "whatever";
if( !strcmp( choice, "No" ) )
{
// choice is equal to "No"
}
2) If you're able to use C++, ditch the char arrays (they suck) and use actual strings instead:
1 2 3 4 5 6
std::string choice = "whatever";
if( choice == "No" )
{
// choice is equal to "No"
}
Note that you cannot use strcmp with strings.
And you cannot use the == operator with char arrays.
Also if you use strings, you can't use gets() to get the input (which you shouldn't do anyway because it's extremely unsafe), but instead you would use iostream's cin object.
Here's the more typical C++ way to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream> // <- iostream
usingnamespace std; // <- for the lazy
int main() // <- main must always return an int, not a void
{
string choice;
cout << "Continue?" << endl;
getline( cin, choice );
if(choice == "No")
{
cout << "ok!" << endl;
}
}
Arrays have no the comparision operator. So this statement
if(choice == "No")
is wrong.
The valid code will be
if ( strcmp( choice, "No" ) == 0 )
All other examples that were showed here in other posts (except the post of @Disch that was written at the same time as main) are invalid. For example, function stricmp is not a standard C++ function. A call of function memcmp as
memcmp(choice,"NO",2))
will not get the correct result if choice has for example value as "NOXX"
(1) All other examples that were showed here in other posts are invalid. For example, function stricmp is not a standard C++ function. A call of function memcmp as
memcmp(choice,"NO",2))
!memcmp(choice,"NO",2))
(2) will not get the correct result if choice has for example value as "NOXX"
Dude do you know what is the meaning of IF exactly?! of course it wont work with other values so listen to me, coming here and showing off is not the point ok?
I think that it is clear enough what I meant. I meant that the code you suggested is invalid. And I showed why it is invalid.
(!memcmp(choice,"NO",2))
The expression returns true even if choice is not equal to "NO". For example if choice is equal to "NOXX" your expression will return true. It is invalid code.
@Peter87
I wouldn't call it it invalid, but it doesn't do what the OP asked for. It only checks the first 2 characters in choice and doesn't care about the rest.
Peter87 if a code does not do what is required it is an invalid code by definition.:)
I do not even know how many times I should repeat to you that ypu will understand that your expression will return true even if choice has value "NOXX"
Try to understand yourself whether string literal "NOXX" is equal to string literal "NO".
If you do not want to read my posts then at least read the post of Disch
By the way, this code
char choice[5];
!memcmp(choice,"NO",5));
is also invalid. For example let imagine that choice contains "NO". But what about other symbols that follow the string literal?! They can be of any value. For example
The following call to "std::memcmp( )" returns 0 indicating that the two string operands are equal:
std::memcmp( "NOE", "NOXX", 2 );
The 3rd parameter forces the comparison of the first 2 characters, thereby excluding the subsequent characters from the comparison. The following expression:
if( !std::memcmp( "NOE", "NOXX", 2 ) )
...will be true.
As Disch said, this isn't the correct function to use to compare strings anyway.
You should stop being so defensive and accept that maybe you were approaching the problem the wrong way this time. Even the best programmers don't always know the best approach to every problem. I've been programming for over half my life and I'm still wrong plenty of times... both on and off these boards. When people correct me I'm always grateful.
If you are refusing to accept that your solution might have been incorrect, and you refuse to accept the advice of people more experienced than yourself... it will be much more difficult for you to grow and improve as a programmer.
So I recommend you chill out and listen to vlad, Framework, Peter, and myself. memcmp is simply the wrong tool here.
But honestly, I do prefer strcmp because you don't have to think about getting the third parameter right.
There is also another problem that there is magic number 3. It is not known the nature of this number. Whether it is the size of array choice or it is the size of the string literal "NO" or this number has some other meaning.