silentvictor Posted October 17, 2013 at 04:01 PM Report #529426 Posted October 17, 2013 at 04:01 PM (edited) Comecei a utilizar ponteiros hoje, um dos trabalhos é de receber um numero e imprimir o seu nome por extenso... No caso, o ponteiro entra na parte do nome dele por extenso. Dai segui o código que foi passado em sala mas ele sempre da esse erro, ja revi várias coisas mas não consigo ver o porque do erro =/ #include <stdio.h> #include <stdlib.h> #include <string.h> int cont=0,b; char numero[3]; char *extenso = NULL; extenso = (char*)malloc(35*sizeof(char)); void unid(){ switch(numero[b]){ case '1':if(cont>=2)strcat(extenso,"e");strcat(extenso," um ");break;//Caso exista apenas unidades case '2':if(cont>=2)strcat(extenso,"e");strcat(extenso," dois ");break; case '3':if(cont>=2)strcat(extenso,"e");strcat(extenso," tres ");break; case '4':if(cont>=2)strcat(extenso,"e");strcat(extenso," quatro ");break; case '5':if(cont>=2)strcat(extenso,"e");strcat(extenso," cinco ");break; case '6':if(cont>=2)strcat(extenso,"e");strcat(extenso," seis ");break; case '7':if(cont>=2)strcat(extenso,"e");strcat(extenso," sete ");break; case '8':if(cont>=2)strcat(extenso,"e");strcat(extenso," oito ");break; case '9':if(cont>=2)strcat(extenso,"e");strcat(extenso," nove ");break; } } void dezen(){ if(numero[b]=='1'){ switch(numero[b-1]){ case '0':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," dez ");break; case '1':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," onze ");break;//verifica caso dgito seja um case '2':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," doze ");break; case '3':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," treze ");break; case '4':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," quatorze ");break; case '5':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," quinze ");break; case '6':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," dezesseis ");break; case '7':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," dezessete ");break; case '8':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," dezoito ");break; case '9':b--;if(cont>=3)strcat(extenso,"e");strcat(extenso," dezenove ");break; } } else{ switch(numero[b]){//se for zero nao faz nada case '2':if(cont>=3)strcat(extenso,"e");strcat(extenso," vinte ");break; case '3':if(cont>=3)strcat(extenso,"e");strcat(extenso," trinta ");break;//Dezena diferente de UM case '4':if(cont>=3)strcat(extenso,"e");strcat(extenso," quarenta ");break; case '5':if(cont>=3)strcat(extenso,"e");strcat(extenso," cinquenta ");break; case '6':if(cont>=3)strcat(extenso,"e");strcat(extenso," sessenta ");break; case '7':if(cont>=3)strcat(extenso,"e");strcat(extenso," setenta ");break; case '8':if(cont>=3)strcat(extenso,"e");strcat(extenso," oitenta ");break; case '9':if(cont>=3)strcat(extenso,"e");strcat(extenso," noventa ");break; } } } void cent(){ switch(numero[b]){//se for zero nao faz nada case '1':if(numero[b-1]=='0'&&numero[b-2]=='0')strcat(extenso," cem "); else strcat(extenso," cento ");break;//verifica caso dgito seja um case '2':strcat(extenso," duzentos ");break; case '3':strcat(extenso," trezentos ");break; case '4':strcat(extenso," quatrocentos ");break; case '5':strcat(extenso," quinhentos ");break; case '6':strcat(extenso," seiscentos ");break; case '7':strcat(extenso," setecentos ");break; case '8':strcat(extenso," oitocentos ");break; case '9':strcat(extenso," novecentos ");break; } } void inverter(char dados[3]) { char auxc; int ac,bc,contc; contc=strlen(dados); bc=contc; for (ac=0;ac<contc/2;ac++){ bc--; auxc=numero[ac]; numero[ac]=numero[bc]; numero[bc]=auxc; } } void tamanhochar(){ printf("Digite valor:"); scanf("%s", &numero); cont=strlen(numero); inverter(numero); if(numero[0]=='0'&&cont<=1){ strcat(extenso,"Zero"); } } int main(int narg, char *argv[]){ printf("%d",narg); if (narg>1){ FILE *arq; arq = fopen(argv[1], "r"); if (arq == NULL) { printf("Erro ao abrir arquivo.\n"); return EXIT_FAILURE; } while(!feof(arq))//Começa loop para ler o arquivo até o seu fim. { fscanf(arq, "%s",&numero);//Lê a linha seguinte e guarda seu valor em "entrada" printf("Digite valor:"); cont=strlen(numero); inverter(numero); if(numero[0]=='0'&&cont<=1){ strcat(extenso,"Zero"); } for(b=cont;b>=0;b--){ switch (b){ case 0:unid();break;//verifica qual casa decimal esta a correr //; case 1:dezen();break; case 2:cent();break; } } } } if (narg<1) { tamanhochar(); for(b=cont;b>=0;b--) { switch (b) { case 0:unid();break;//verifica qual casa decimal esta a correr //; case 1:dezen();break; case 2:cent();break; } } } printf("%s", extenso); return EXIT_SUCCESS; } Edited October 17, 2013 at 08:42 PM by thoga31 GeSHi
Flinger Posted October 17, 2013 at 04:23 PM Report #529437 Posted October 17, 2013 at 04:23 PM Posta aqui o output da compilação, incluindo a linha ou linhas onde te dá o erro.
silentvictor Posted October 17, 2013 at 04:33 PM Author Report #529440 Posted October 17, 2013 at 04:33 PM teste.c:7:1: aviso: data definition has no type or storage class [habilitado por padrão] teste.c:7:1: erro: conflicting types for ‘extenso’ teste.c:6:7: nota: previous definition of ‘extenso’ was here teste.c:7:11: aviso: initialization makes integer from pointer without a cast [habilitado por padrão] teste.c:7:1: erro: initializer element is not constant teste.c: Na função ‘unid’: teste.c:11:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:11:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:12:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:12:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:13:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:13:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:14:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:14:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:15:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:15:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:16:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:16:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:17:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:17:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:18:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:18:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:19:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:19:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c: Na função ‘dezen’: teste.c:25:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:25:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:26:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:26:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:27:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:27:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:28:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:28:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:29:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:29:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:30:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:30:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:31:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:31:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:32:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:32:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:33:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:33:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:34:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:34:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:39:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:39:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:40:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:40:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:41:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:41:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:42:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:42:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:43:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:43:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:44:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:44:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:45:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:45:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:46:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:46:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c: Na função ‘cent’: teste.c:53:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:53:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:54:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:55:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:56:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:57:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:58:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:59:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:60:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:61:51: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c: Na função ‘tamanhochar’: teste.c:79:1: aviso: formato ‘%s’ espera argumento do tipo ‘char *’, porém o argumento 2 possui tipo ‘char (*)[3]’ [-Wformat] teste.c:83:34: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c: Na função ‘main’: teste.c:101:4: aviso: formato ‘%s’ espera argumento do tipo ‘char *’, porém o argumento 3 possui tipo ‘char (*)[3]’ [-Wformat] teste.c:106:10: aviso: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [habilitado por padrão] /usr/include/string.h:136:14: nota: expected ‘char * __restrict__’ but argument is of type ‘int’ teste.c:131:1: aviso: formato ‘%s’ espera argumento do tipo ‘char *’, porém o argumento 2 possui tipo ‘int’ [-Wformat]
Flinger Posted October 17, 2013 at 05:20 PM Report #529456 Posted October 17, 2013 at 05:20 PM Credo, já há muito tempo não via um programa com tanto warning. Vamos por partes, char *extenso = NULL; extenso = (char*)malloc(35*sizeof(char)); Declaras a variável extenso, e a seguir estás a tentar inicializá-la fora do sítio. A segunda linha tem de passar para a main. (Já que é uma global. Deixo para o Hippo dar-te nas orelhas por causa disso 😛 ) Depois, se ainda tiveres esses warnings todos, tenta ir a todos os strcat, e colocar os casts no segundo elemento: strcat(extenso,(const char *) "e") Não tenho a certeza se os warnings são do primeiro (por causa da declaração marada) ou 2.º parâmetro. Tenta o primeiro passo, compila, e se se mantiverem esses warnings todos testa o segundo. Ainda vais ter mais erros, mas vai-se limpando aos poucos. EDIT: A ler melhor o output, parece-me que é mesmo só por causa do primeiro argumento, por isso não deves precisar do segundo passo. Quando tiveres o output da compilação corrigida, posta aqui.
HappyHippyHippo Posted October 17, 2013 at 05:30 PM Report #529458 Posted October 17, 2013 at 05:30 PM é só para dar nas orelhas (estou no telemóvel e não consigo escrever muito) ... logo, quando tiveres isso a correr sem bugs, apresento-te um código mais "limpinho" IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
silentvictor Posted October 17, 2013 at 05:40 PM Author Report #529459 Posted October 17, 2013 at 05:40 PM (edited) Tenho que melhorar essas variaveis, sempre tenho problema com elas 😞 Sobre o erro/warnings, estão todos ok 🙂 A unica coisa que diz agora é: teste.c: In function ‘tamanhochar’: teste.c:79:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[3]’ [-Wformat] teste.c: In function ‘main’: teste.c:101:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[3]’ [-Wformat] E o programa está compilando e executando normalmente... Exceto o TXT(Segmentation fault ), que deve ser algum errinho nas variaveis, vou dar uma olhada. Muito obrigado pela ajuda, mas pode me explicar o motivo de strcat(extenso,(const char *) "e") Edited October 17, 2013 at 05:43 PM by silentvictor
HappyHippyHippo Posted October 17, 2013 at 05:47 PM Report #529460 Posted October 17, 2013 at 05:47 PM scanf("%s", &numero); // <--- que tipo de variavel é "numero" ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
silentvictor Posted October 17, 2013 at 05:49 PM Author Report #529462 Posted October 17, 2013 at 05:49 PM Ele é um vetor do tipo char .-. Mesmo colocando %c o erro continua =/
HappyHippyHippo Posted October 17, 2013 at 05:54 PM Report #529463 Posted October 17, 2013 at 05:54 PM é um ponteiro (referência de memória) !!! logo não necessitas do operador '&' IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
silentvictor Posted October 17, 2013 at 06:02 PM Author Report #529469 Posted October 17, 2013 at 06:02 PM Bem, agora está tudo sem erro .-. E não vou mais acreditar no pessoal da minha sala... Coloquei algumas coisas ai seguindo a ajuda deles e praticamente tudo que botei foi o que gerou erro, exceto a variavel global. O unico problema é quando rodo o TXT agora... da um erro de segmentação
HappyHippyHippo Posted October 17, 2013 at 06:19 PM Report #529471 Posted October 17, 2013 at 06:19 PM verifica se o valor foi lido correctamente do ficheiro validando o valor de retorno : http://linux.die.net/man/3/scanf IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
HappyHippyHippo Posted October 25, 2013 at 08:12 AM Report #530511 Posted October 25, 2013 at 08:12 AM (edited) logo, quando tiveres isso a correr sem bugs, apresento-te um código mais "limpinho" como o último post do criador deste thread já leva uma semana, para referencia futura de quem necessita de executar uma aplicação deste género aqui fica um código bem mais "limpo" // gcc -o por_extenso main.c #include <stdio.h> #include <stdlib.h> #include <string.h> char * UNIDADES[] = {"um", "dois", "tres", "quatro", "cinco", "seis", "sete", "oito", "nove"}; char * DEZENAS[] = {"dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa"}; char * CENTENAS[] = {"cento", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"}; char * TEENS[] = {"onze", "doze", "treze", "quatorze", "quinze", "dezasseis", "dezassete", "dezoito", "dezanove"}; char ** EXTENSO[] = {UNIDADES, DEZENAS, CENTENAS}; void por_extenso(char * value, int size) { int number, index, serie = 0, has_prev = 0; sscanf(value, "%d", &number); switch (number) { // pesky irregular number cases case 0: printf("zero"); break; case 100: printf("cem"); break; // regular number cases + teens default: for (index = 0; index < size; index++) { if (value[index] != '0') { if (has_prev) printf(" e "); serie = size - index - 1; // teen number check if (serie == 1 && value[index] == '1' && value[index + 1] != '0') { printf("%s", TEENS[value[index + 1] - '1']); break; } else { printf("%s", EXTENSO[serie][value[index] - '1']); has_prev = 1; } } } break; } printf("\n"); } int main(int argc, char ** argv) { int ndigits = 0; if (argc != 2) { printf("ERROR > command : por_extenso <positive number:max 999>\n"); return EXIT_FAILURE; } do { // discard string with alphanumirics, ex : "12a" // and max = 999 if (argv[1][ndigits] < '0' || argv[1][ndigits] > '9' || ndigits == 3) { printf("ERROR > command : por_extenso <positive number:max 999>\n"); return EXIT_FAILURE; } ndigits++; } while (argv[1][ndigits] != 0); por_extenso(argv[1], ndigits); return EXIT_SUCCESS; } Edited October 25, 2013 at 08:13 AM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
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