if problem!



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?


#include <stdio.h>
void main()
{
char choice[5];

printf("Continue?\n");
gets(choice);
if(choice == "No")
{
printf("ok!\n",choice);
}
}
Last edited on
You conditional logic is wrong. As when the two strings are the same the result is zero, but this then equates to FALSE. So you need:

 
if (choice == "No" == 0)
Last edited on
use this

1
2
3
4
 if(!memcmp(choice,"NO",2))
{
    printf("ok!\n",choice);
}


READ THIS http://www.cplusplus.com/reference/cstring/memcmp/
Last edited on
uhm.... joneele care explain what is this? cuz i never used iostream....
Or you could use stricmp no no-case comparisen:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <string.h>

int main()
{
	char choice[5];
 
	printf("Continue?\n");
	gets(choice);
	
	if (stricmp(choice, "no") == 0)
	{
		printf("ok!\n",choice);
	}

	return 0;
}
memcmp is not the right tool here.

You have 2 choices:

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
using namespace 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"


Last edited on
vlad What do you mean?!

(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?
Last edited on
@joneele
vlad What do you mean?!


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.

I hope that you have understood what I wrote.

Last edited on
I wouldn't call 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.
Last edited on
@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.:)
vald? seriously?????

1
2
char choice[2];
!memcmp(choice,"NO",2));


how tha ___ you gonna add more than 2 characters?
so it's not invalid as you said...
@joneele
Why did you change the size of choice?
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".
@peter87, It does not make any different i just wanted to explain that what he said is wrong

@vlad I know what u said but you cannot say invalid

1
2
char choice[5];
!memcmp(choice,"NO",5));


?????
Last edited on
I already showed how it shall be done

if ( strcmp( choice, "NO" ) == 0 )

The same was showed also by Disch

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

choice == 'N', 'O', '\0', 'S' 'D'
"NO" == 'N', 'O', '\0', '1', '2'

You are trying to compare some garbage.
Last edited on
closed account (zb0S216C)
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.

Wazzak
Last edited on
!memcmp(choice,"NO",5));
This is not safe. In some cases it would try to read outside the string literal array.

!memcmp(choice,"NO",3));
This works much better and will do how the OP and vlad wants it.

But honestly, I do prefer strcmp because you don't have to think about getting the third parameter right.
@ joneele:

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.
Last edited on
@Peter87

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.
Topic archived. No new replies allowed.