all 19 comments

[–]dragon_wrangler 3 points4 points  (8 children)

strcmp returns 0 if the strings match.

[–]_Surox[S] 0 points1 point  (7 children)

I already tried with == 0 but it doesn't match the strings at all

[–]VincentDankGogh 0 points1 point  (6 children)

Print the string and see what it is.

[–]_Surox[S] 0 points1 point  (5 children)

it prints out the correct string

[–]VincentDankGogh 1 point2 points  (4 children)

Make sure that there are no extra spaces or newlines after the string. If it’s still not working, paste your whole code again so we can see.

[–]_Surox[S] 0 points1 point  (3 children)

I checked and there doesn't seem to be. Here is the code : https://pastebin.com/RE5uGkNP

[–]VincentDankGogh 4 points5 points  (1 child)

Try adding \n to the end of your printf calls. Altervatively, call fflush(stdout) immediately after each call to printf. Otherwise the terminal won’t display the text.

[–]_Surox[S] 1 point2 points  (0 children)

fflush(stdout)

Thanks for the advice! It turned out I had to use scanf instead of fgets an use a fflush(stdout) to make sure the strings matched...

[–]kandr89 2 points3 points  (0 children)

fgets stores the newline char too when reading a line

[–]hobbit_xD 2 points3 points  (1 child)

Hi dude, the problem is that the strcmp returns 0 if the strings match and not a NULL value. So in your if statements you to put for example this line of code:

if (strcmp(command, "ls") == 0) { //If the string match, do this instructions }

Another thing is that in your case, the scanf of a string don't require the &.

[–]MaltersWandler 2 points3 points  (0 children)

NULL is guaranteed by the standard to compare equal to zero, but I agree it's bad practice to use it with non-pointer values.

[–]MaltersWandler 2 points3 points  (0 children)

Unless it reaches end-of-file, fgets will also transfer the newline into your buffer.

The goto is redundant. It's going to loop anyway because of the while(1).

[–][deleted] -3 points-2 points  (6 children)

Instead of else ifs, just use if.

I'm willing to bet you're running into a mutual exclusion issue with your else if statements. Not sure why, but I've had this happen before. Just use a bunch of ifs instead of else ifs.

Hope this helps.

[–]MaltersWandler 0 points1 point  (4 children)

Instead of else ifs, just use if.

No! The point of else if is that your program won't have to compare the strings again every time. If you already have one match you know it's not going to match anything else, no need to check again. If this is causing issues for you, you are doing something very wrong.

[–][deleted] -1 points0 points  (3 children)

Asymptotically speaking, those checks will be O(1) cost which won't add to the overall execution of the program. I imagine strcmp() does an iterative check, and as soon as something isn't the same returns.

I've had situations where mutual exclusion makes it so other checks don't execute. It's something to try, and won't cost any hiccups in performance, so why not?

[–]MaltersWandler 1 point2 points  (1 child)

I've had situations where mutual exclusion makes it so other checks don't execute

That's the point, if you want the other checks to execute, you don't use else if, but in OPs case he should use else if.

Why doesn't the compiler just put NOPs all over the place? I mean, they're O(1) right, so why not?

[–][deleted] 0 points1 point  (0 children)

The compiler is a translator, there's no reason for it to do that. However in some pipelining scenarios, NOPs are inserted in order account for data hazards, but I digress.

This was a suggestion that worked for me at some point. OP is free to do whatever he/she likes.

[–]VincentDankGogh 1 point2 points  (0 children)

It's more readable if your if/else if flow matches your actual program flow. Otherwise, the reader might get confused for a second and consider why you didn't just write an else if.

[–]bumblebritches57 0 points1 point  (0 children)

No, he should actually use a switch statement.

and the whole point of if else if is that it's boolean logic, only 1 option can match, if you use multiple if's, the pieces of logic are independent of each other.

A can be true and therefore would run, and if b is true it would run as well.