Error comparing two strings C

1

I enter a value on the command line. That is in binary vector. But if it is not returned to the printf message. But my problem is that it always goes into if , regardless of whether it is binary or not.

#include <stdio.h>
#include <string.h>
#include "ex02.h"

int main(){

 char binario [50][100]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};

 char nome [0][10];

 int contador=0;

  printf("Insira um valor\n");
  scanf("%s",nome[0]);

while(nome[contador]!='
#include <stdio.h>
#include <string.h>
#include "ex02.h"

int main(){

 char binario [50][100]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};

 char nome [0][10];

 int contador=0;

  printf("Insira um valor\n");
  scanf("%s",nome[0]);

while(nome[contador]!='%pre%'){

   if ( strcmp(nome[0], binario[contador]) !=0 )
        {
            printf("Base inicial invalida");
            break;
        }

        contador++;
 } 

 return 0;      
'){ if ( strcmp(nome[0], binario[contador]) !=0 ) { printf("Base inicial invalida"); break; } contador++; } return 0;
    
asked by anonymous 24.09.2016 / 10:37

2 answers

2

The code has several flaws and does not even close what you are asking in the question. I'll try to fix it.

Note that the arrays statements can be made to the size they are required. This gives more efficiency. So binario only needs 16 elements and each of them only needs 5 bytes (4 characters + the terminator). nome only needs the array of characters, there has to be another dimension. No dimension can have size 0.

I preferred to use for , although while is not wrong.

The if is quite wrong. To find out if you have not found a text within the previously defined list, you have to analyze all the items. The current code is parsing the first item, if it is not equal it ends the loop and ends the search. In other words, this code requires everyone to be equal to function. It's not what you want. The solution is to let them search to the end, or even find one of them. If you find one you do not have to continue searching, just one to satisfy what you want.

But how do you know if you did not find it? It's simple, if he goes through all and never gets out of the loop he's never found it. To know that this occurred, just check that the counter has gone through all the elements and reached 16. Even if you find in the last element the break would force the output and the last increment would not be made, there the counter would be worth 15.

There are other problems in the code that I will not treat.

#include <stdio.h>
#include <string.h>

int main() {
    char binario[16][5] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
    char nome[10];
    printf("Insira um valor\n");
    scanf("%s", nome);
    int contador = 0;
    for (; contador < 16; contador++) {
        if (strcmp(nome, binario[contador]) == 0) {
            break;
        }
    } 
    if (contador == 16) {
        printf("Base inicial invalida"); //essa mensagem não faz sentido com o enunciado
    }
}

See running on ideone .

    
26.09.2016 / 15:00
1

According to your explanation, you should analyze the logic of your code to function according to the operating rules of each API. In the case of strcmp we have the following operation rule:

int strcmp(
   const char *string1,
   const char *string2 
);
Return Value
The return value for each of these functions indicates the ordinal relation of string1 to string2.
Value Relationship of string1 to string2
< 0   string1 is less than string2
  0   string1 is identical to string2
> 0   string1 is greater than string2

So your code goes into the IF because something other than zero is always returned, since in any comparison it finds an identical value.

    
24.09.2016 / 21:47