d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Noobie C Error > Strcmp
Add Reply New Topic New Poll
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 4 2013 10:13pm
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 if i enter y
can someone tell me whats the issue?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2013 10:15pm
i recommend you learn how to debug.

char str3[20];

fgets(str3, 20, stdin);
print(str3);
int comparison = strcmp(str3, "y");
print(comparison)
if(strcmp(str3,"y")==0){
//do something
}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 4 2013 10:23pm
Quote (carteblanche @ Oct 4 2013 06:15pm)
i recommend you learn how to debug.

char str3[20];

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



this doesnt help me if i dont know why im getting a non zero
i already know strcmp is not returning zero

This post was edited by bakalolo on Oct 4 2013 10:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 4 2013 10:43pm
Quote (bakalolo @ Oct 5 2013 12:23am)
this doesnt help me if i dont know why im getting a non zero
i already know strcmp is not returning zero


i'm no C expert, but your buffer size of 20 seems suspicious to me since you're comparing it to something with size 1. how do you determine how many characters you're reading in? either you're terminating after 20 characters or you're signaling fgets to terminate early somehow. try running strlen on the input and strlen of "y" and see what you get. i suspect you're telling fgets to stop after "y" by pressing the enter button, which might produce a newline character. so strlen(str3) = 2 from y\n (or possible 3 if y\r\n) instead of 1 that you expect.

but if strlen(str3) is 1 and strlen("y") is 1 and printing them both out look the same, then print out the ascii/unicode/binary. keep debugging until you're 100% sure they're the same. if it comes to that point, see if maybe there's another comparison function to use. and if you're still not making progress, i'm out of ideas. then wait for someone else with more C experience to point out the problem.

/edit: and of course, try out strcmp("y", "y") and make sure that returns what you expect.

This post was edited by carteblanche on Oct 4 2013 11:03pm
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Oct 4 2013 11:26pm
buffer size should be fine as long as a newline character is added right after the input. Try setting the second character to the null character. Also print the input string and see what it looks like.

This post was edited by zackill4 on Oct 4 2013 11:27pm
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Oct 4 2013 11:32pm
Quote (zackill4 @ Oct 4 2013 07:26pm)
buffer size should be fine as long as a newline character is added right after the input. Try setting the second character to the null character. Also print the input string and see what it looks like.


yea that fixed it thnx
Banned
Posts: 4,377
Joined: Dec 25 2012
Gold: 13,953.01
Warn: 10%
Oct 4 2013 11:35pm
Quote (bakalolo @ Oct 5 2013 12:32am)
yea that fixed it thnx


lol, kinda hacky but nice
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll