thinkabout Posted May 11, 2017 at 11:33 PM Report #604162 Posted May 11, 2017 at 11:33 PM (edited) Strlen não devolve 0 Se meter o nome "a" - Strlen devolve 1 Se não meter nada (empty) Strlen devolve 1 também. Como posso detectar emptys strings ? int = 0; do { puts("Qual e o ID da area?"); fgets(buffer.idarea, MAX, stdin); strtok(buffer.idarea, "\n"); // Consumir o \n printf("O tamanho da string que apanhei e %d\n", tam = strlen(buffer.idarea)); } while (verifica_area_duplicadas(vector, *total, buffer.idarea) == 0); // Verificar se o ID que o utilizador inseriu já existe e se a string é empty. Edited May 11, 2017 at 11:35 PM by thinkabout
HappyHippyHippo Posted May 12, 2017 at 06:57 AM Report #604163 Posted May 12, 2017 at 06:57 AM o strlen devolve 0 case realmente a string estiver vazia, o que não aconteve com o teu fgets. a tua string não está vazia- tem o caracter '\n' e isso conta IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 10:42 AM Author Report #604166 Posted May 12, 2017 at 10:42 AM (edited) Não estou bem a ver como resolver o problema. Pois string com "\n" ou "a\n", devolve-me o mesmo número de caracteres. Já tentei na condição ( condição 1 || buffer.idarea[0] != '\n' ), mas sem sucesso. Edited May 12, 2017 at 10:51 AM by thinkabout
HappyHippyHippo Posted May 12, 2017 at 12:08 PM Report #604170 Posted May 12, 2017 at 12:08 PM ??? estás a dizer que o output do seguinte exemplo: #include <stdio.h> #include <string.h> int main(void) { printf("%d\n", strlen("\n")); printf("%d\n", strlen("a\n")); return EXIT_SUCCESS; } é 1 1 ??? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 03:53 PM Author Report #604188 Posted May 12, 2017 at 03:53 PM int main(void) { char buffer[100]; int tamanho; fgets(buffer,100,stdin); // Só dei enter aqui strtok(buffer, "\n"); printf("%d\n", tamanho = strlen(buffer)); // Output 1 fgets(buffer,100,stdin); // Escrevi caracter "a" strtok(buffer, "\n"); printf("%d\n", tamanho = strlen(buffer)); // Output 1 return EXIT_SUCCESS; }
HappyHippyHippo Posted May 12, 2017 at 04:36 PM Report #604196 Posted May 12, 2017 at 04:36 PM ve o que realmente estas a fazer : #include <stdlib.h> #include <stdio.h> #include <string.h> int main(void) { char buffer[100]; int tamanho; fgets(buffer,100,stdin); // Só dei enter aqui strtok(buffer, "\n"); printf("|%s|%d\n", buffer, tamanho = strlen(buffer)); // Output 1 fgets(buffer,100,stdin); // Escrevi caracter "a" strtok(buffer, "\n"); printf("|%s|%d\n", buffer, tamanho = strlen(buffer)); // Output 1 return EXIT_SUCCESS; } por outras palavras, strtok não serve para tirar '\n', pode ter esse efeito em alguns casos, mas como podes ver, não é a melhor solução algo como o seguinte seria bem mais fiavel buffer[strlen(buffer) - 1] = 0; (isto depois de validares o resultado do fgets, claro ...) IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 05:24 PM Author Report #604200 Posted May 12, 2017 at 05:24 PM (edited) 47 minutos atrás, HappyHippyHippo disse: por outras palavras, strtok não serve para tirar '\n', pode ter esse efeito em alguns casos, mas como podes ver, não é a melhor solução algo como o seguinte seria bem mais fiavel buffer[strlen(buffer) - 1] = 0; (isto depois de validares o resultado do fgets, claro ...) do { puts("Qual e o ID da area?"); if (fgets(buffer.idarea, sizeof (buffer.idarea), stdin) == NULL) break; buffer.idarea[strlen(buffer.idarea) - 1] = '\0'; // Limpar o /n } while ( (int) strlen(buffer.idarea) == 0 || verifica_area_duplicadas(vector, *total, buffer.idarea) == 0); Agora só tenho que ver como impeço que a minha string tenha espaços brancos no inicio e que só tenha em conta a 1 palavra de forma a não existir "AreaC Amarela", e só existir AreaC. Edited May 12, 2017 at 05:25 PM by thinkabout
HappyHippyHippo Posted May 12, 2017 at 05:53 PM Report #604201 Posted May 12, 2017 at 05:53 PM sscanf IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 06:51 PM Author Report #604206 Posted May 12, 2017 at 06:51 PM 37 minutos atrás, HappyHippyHippo disse: sscanf Está quase lá... int num = 0; while (num == 0) { puts("Qual e o ID da area?"); if (fgets(buffer.idarea, sizeof (buffer.idarea), stdin) == NULL) break; if (sscanf(buffer.idarea, "%s", &num) != 1) { printf("string invalida\n"); continue; } } Só consigo fazer com que ele ignore uma serie espaços branco no inicio. 1º Teste - "" - Ok 2º Teste - " "Ok 3º Teste - " XPTO" - Aceita a string mas aceita também espaços em brancos. 4º Teste - "AreaA Verde" - Aceita as duas palavras, também não queria isto.
HappyHippyHippo Posted May 12, 2017 at 06:52 PM Report #604208 Posted May 12, 2017 at 06:52 PM usa o seguinte formato do sscanf : " %s" IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 06:55 PM Author Report #604210 Posted May 12, 2017 at 06:55 PM 1 minuto atrás, HappyHippyHippo disse: usa o seguinte formato do sscanf : " %s" Já o tinha feito mas sem sucesso.
HappyHippyHippo Posted May 12, 2017 at 06:55 PM Report #604211 Posted May 12, 2017 at 06:55 PM Just now, thinkabout said: Já o tinha feito mas sem sucesso. e que sucesso tiveste ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 07:00 PM Author Report #604212 Posted May 12, 2017 at 07:00 PM 4 minutos atrás, HappyHippyHippo disse: e que sucesso tiveste ? A mesma coisa. while (num == 0) { puts("Qual e o ID da area?"); if (fgets(buffer.idarea, sizeof (buffer.idarea), stdin) == NULL) break; if (sscanf(buffer.idarea, " %s", &num) != 1) { num = 0; printf("string invalida\n"); continue; } }
HappyHippyHippo Posted May 12, 2017 at 07:03 PM Report #604214 Posted May 12, 2017 at 07:03 PM afinal que raio de variável é num ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 07:04 PM Author Report #604215 Posted May 12, 2017 at 07:04 PM Para ele repetir a pergunta enquanto não tiver o formato correcto no sscanf
HappyHippyHippo Posted May 12, 2017 at 07:05 PM Report #604216 Posted May 12, 2017 at 07:05 PM então porque razão está como terceiro argumento do sscanf ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
thinkabout Posted May 12, 2017 at 07:24 PM Author Report #604217 Posted May 12, 2017 at 07:24 PM (edited) 20 minutos atrás, HappyHippyHippo disse: então porque razão está como terceiro argumento do sscanf ? int teste = 0; do { while (teste == 0) { puts("Qual e o ID da area?"); if (fgets(buffer.idarea, MAX, stdin) == NULL) break; if (sscanf(buffer.idarea, " %s", buffer.idarea) != 1) { printf("string invalida\n"); } else { // String e valida teste = 1; } } // buffer.idarea[strlen(buffer.idarea) - 1] = '\0'; // Limpar o /n } while ((int) strlen(buffer.idarea) == 0 || verifica_area_duplicadas(vector, *total, buffer.idarea) == 0); O sscanf remove o '\n' ? Mantendo esta linha estava com alguns problemas no input. Quando fazia tipo Area1234, o que era depois inserido era Area123... buffer.idarea[strlen(buffer.idarea) - 1] = '\0'; // Limpar o /n Edited May 12, 2017 at 07:25 PM by thinkabout
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now