Does anyone have any idea why the if not be executed?

Asked

Viewed 50 times

0

if (strcmp(letra4, "[") == 0) {
  char string_sem_colchete[10];
  for(i=1; i<strlen(comando4)-1; i++)
  {
    string_sem_colchete[i-1] = comando4[i];
  }

  string_sem_colchete[i-1] = '\0';
  numero4 = atoi(string_sem_colchete);
  printf("%i", numero4);
}

In summary the variable letra4 is the first position of a string, if the character of that position is "[", it will execute that code within the if to remove the first and last character of that string and convert into int. The code works, the problem is that the IF appears not to be executed.

3 answers

0

You just want to compare the first char? strcmp compares the entire string. It would only pass in if its string was only "[".

Compare only the first position: if (letra4[0] == '[')... This imagining that letra4 is either a vector or a pointer, because you left the mystery in the air right?

  • that, I’ve already tried to do this, but it wasn’t tbm, so I stored the first character of another string in the letter char 4, I print this variable and it appears "[", so I don’t have the slightest idea of why the if not run

0


How does it mention that the variable letra4 is the first position of a string, I believe you want to compare chars. If this is the case, comparing the values directly should be sufficient (note that I am using single quotes, this denotes the type char and not string):

if (letra4 == '[') {

The comparison you’re trying to make with strcmp currently should receive two pointers (strings or char*) in your call. It would work if you were passing char* (just as an example, remember to allocate and release the memory used):

char* letra4 = "[";

Using pointers and not char, you can make the comparison with strcmp.


And your case string be a array of chars literal:

char letra4[20] = {/*...*/};

You can compare only the first position of your array:

if (letra4[0] == '[') {

I hope I’ve helped in some way.

  • our I had no idea that simple quotes were for char and double quotes was for strings, for me it was all the same thing kkkkkkkkkkkkkkkk thank you very much, I’ll see if it works aq

  • i tried using simple quotes, tried to compare the other string with positions [0], [1], [2]... but nothing, then I tried to make a simple if(letra4 ='['){} and apparently it worked kkkkkkkkkkkk

0

If you are not entering the IF, I believe that 'letra4' is presenting another value. Try to put breakpoint and access the contents of this variable.

If you have any questions about strcmp doc': http://linguagemc.com.br/a-biblioteca-string-h/

  • type like this, letra4 is the first character of a string... letra4 = command[0], I print the variable letra4, and what appears is "[", so I have no idea why the if is not executed

  • In this case you have a char.... and not a char[]... Voce could do if like this: if (letra4 == '[' ), with simple quotes... maybe that’s your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.