The code has several flaws and does not even closely what you are asking in the question. I will try to fix.
Note that the statements of arrays can be made to the size they are needed. This gives more efficiency. So binario
only needs 16 elements and each of them only needs 5 bytes (4 characters + the terminator). nome
just needs the array character, there is no other dimension. No dimension can be 0.
I preferred to use the for
, although the while
don’t be wrong.
The if
is quite wrong. To know if n]ao found a text within the previously defined list you have to parse all items. The current code is analyzing the first item, if it is not equal it terminates the loop and terminates the search. That is, this code requires everyone to be the same to work. It’s not what you want. The solution is to let you search until the end, or until you find one of them. If you find one you have no reason to continue researching, just one to satisfy what you want.
But how do you know you didn’t? It’s simple, if he goes through all of them and never leaves the noose forcibly, he never did. To know that this occurred it is only to check if the counter went 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, then the counter would be 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 && strcmp(nome, binario[contador]) != 0; contador++);
if (contador == 16) printf("Base inicial invalida"); //essa mensagem não faz sentido com o enunciado
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Are you sure this is the statement? The code does not produce what is in the statement nor closely.
– Maniero
It would be nice to [Edit] and add examples of what data you entered the test, what result you gave, and what results you expect.
– Bacco