d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > C Program Issue > Strcmp
12Next
Add Reply New Topic New Poll
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 4 2013 10:31pm
Code
char str3[20];

fgets(str3, 20, stdin);
if(strcmp(str3,"y")==0){
//do something
}


im reading user input and if it's "y" go through the if
but it doesn't go through the if i enter y
can someone tell me why strcmp is not returning 0?

Member
Posts: 21,656
Joined: Jun 19 2012
Gold: 42,200.53
Oct 4 2013 10:34pm
Well, shouldn't this line:

if(strcmp(str3,"y")==0){

be:

if(strcmp(str3,"y")==1){

?
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 4 2013 10:53pm
Quote (CorinaMarie @ Oct 4 2013 06:34pm)
Well, shouldn't this line:

if(strcmp(str3,"y")==0){

be:

if(strcmp(str3,"y")==1){

?


why would it be 1 though? shouldnt equal value return 0?
but yea that would work since it returns 1 for some reason when i enter y for str3
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2013 10:54pm
Quote (bakalolo @ Oct 5 2013 12:53am)
why would it be 1 though? shouldnt equal value return 0?
but yea that would work since it returns 1 for some reason when i enter y for str3


does it return 1 if you type in z too?
Member
Posts: 21,656
Joined: Jun 19 2012
Gold: 42,200.53
Oct 4 2013 10:55pm
Quote (bakalolo @ Oct 5 2013 12:53am)
why would it be 1 though? shouldnt equal value return 0?
but yea that would work since it returns 1 for some reason when i enter y for str3


1 = True
0 = False
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 4 2013 10:56pm
Quote (carteblanche @ Oct 4 2013 06:54pm)
does it return 1 if you type in z too?


yea it does
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2013 10:56pm
what about if you tried x?

i already suggested steps to debug in your other thread. what were the results?

Quote (CorinaMarie @ Oct 5 2013 12:55am)
1 = True
0 = False


not according to documentation.

This post was edited by carteblanche on Oct 4 2013 10:57pm
Member
Posts: 21,656
Joined: Jun 19 2012
Gold: 42,200.53
Oct 4 2013 10:57pm
Quote (carteblanche @ Oct 5 2013 12:56am)
i already suggested steps to debug in your other thread. what were the results?



not according to documentation.


Ok. It does in an old language I used to use. :lol:

I don't know C, so I'll step out now. ;)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2013 11:00pm
Quote (CorinaMarie @ Oct 5 2013 12:57am)
Ok. It does in an old language I used to use. :lol:

I don't know C, so I'll step out now. ;)


it's not a bad guess. you are correct that 1 is true and 0 is false, but that function actually tells you 3 states, not just 2. it tells you if it's greater, less than, or equal (positive, negative, or 0 respectively). so in this case 1 doesn't mean equal
Member
Posts: 21,656
Joined: Jun 19 2012
Gold: 42,200.53
Oct 4 2013 11:03pm
Quote (carteblanche @ Oct 5 2013 01:00am)
it's not a bad guess. you are correct that 1 is true and 0 is false, but that function actually tells you 3 states, not just 2. it tells you if it's greater, less than, or equal (positive, negative, or 0 respectively). so in this case 1 doesn't mean equal


I see. Thanks for explaining it. So, is there a simple yes/no function instead of using that one?
Go Back To Homework Help Topic List
12Next
Add Reply New Topic New Poll