Thread: Help Please!!!

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    Exclamation Help Please!!!

    As no one have answered me i will post it again if someone could help i would appreciate... PLEASE!!! My brain is already melting :/

    I know it's not a simple question but i really can't understand what is happening :/

    I'm having a problem with a function... everything goes ok till it returns the result because the string get somehow "corrupted"... i have searched for errors but i really didn't find it... it's a complex code, with structs and pointers and but i will copy and paste what i think it's necessary to understan it... If someone could help the error occurs in the line "TF = GetToken();" in the function LTEIns();

    the functions ImpErr(); and ImpErrS(); just print error messages and aren't necessary and for that i didn't post them. The functions VerChFo(); and VerChNo just verify if the chars are valid characters... and if there is some /**/ in the middle it's just things i had changed trying to fix it /

    Code:
    #define INTGR 1
    #define FLT 2
    #define STRG 3
    #define TOK 4
    #define TRUE 1
    #define FALSE 0
    #define ENTER 13
    #define MAXF 10
    #define MAXS 30
    #define NOME 5
    #define FONE 6
    
    typedef struct tokelemento {
            int ttelem;
            char *tpselem;
            }*TOKELEM;
    
    typedef struct tokens {
            TOKELEM nomelem;
            TOKELEM fonelem;
            }*TOKEN;
    
    typedef struct  elemento {
                    int telem;                                  
                    union {
                           int ielem;
                           float felem;                         
                           char *pselem;
                           TOKEN toelem;
                           } elem;
            } *PLISTELEM;                             
    typedef struct lnodo {
                    PLISTELEM litem;
                    struct lnodo *next;                                
            } *PLIST;          
    
    char VerChNo (char *c) {
            while ( ((*c!= ' ') && (*c!= ENTER) && (*c!='"')) && ((*c < 'A') || (*c > 'Z')) && ( (*c < 'a') || (*c > 'z'))) {
                if (*c == 0) {
                   *c = getch();
                   if (*c==10) return ENTER;
                   fflush(stdin);
                   printf ("\b");
                   ImpErr();
                   *c = getche();
                   }
                   else
                       sound(362);
                       delay (500);
                       nosound( );
                       printf ("\b");
                       textcolor(0);
                       putch (219);
                       textcolor(15);
                       printf ("\b");
                       ImpErr();
                       *c = getche();
    
                }
            if ((*c>= 'a') && (*c<='z')) *c = toupper(*c);
            return *c;
            }
    
    char VerChFo (char *c) {
            while ( ((*c!= ' ') && (*c!= ENTER)) && ( (*c < '0') || (*c > '9')) && (*c != '-')) {
                if (*c == 0) {
                   *c = getch();
                   if (*c==10) return ENTER;
                   fflush(stdin);
                   printf ("\b");
                   ImpErr();
                   *c = getche();
                   }
                   else
                       sound(362);
                       delay (500);
                       nosound( );
                       printf ("\b");
                       textcolor(0);
                       putch (219);
                       textcolor(15);
                       printf ("\b");
                       ImpErr();
                       *c = getche();
    
                }
            return *c;
            }
    
    TOKELEM GetToken (void) {
            char c;
            TOKELEM tok=NULL;
            char S[MAXS], F[MAXF];
            int i = 0;
            float fl;
    
            c = getche();
            while ( c == ' ') c=getche();
            while ( (c != ' ' ) && (c!= ENTER) ){
                tok = malloc (sizeof(struct tokelemento));
                if (c=='"') {
                   c=getche();
                   VerChNo(&c);
                   while ( c!='"'){
                         S[i]=c;
                         i++;
                         c=getche();
                         VerChNo(&c);
                         if (i==MAXS-2) {
                            c = '"';
                            ImpErrS(S);
                         }
                        }
                   S[i]='\0';
                   tok->ttelem = NOME;
                   tok->tpselem = S;
                   return tok;
                   }
                   else if (c>='0'&& c<='9'){
                          do {
                              while ( (c != ' ' ) && (c!= ENTER) ){
                                   F[i]=c;
                                   i++;
                                   c=getche();
                                   VerChFo(&c);
                                   if (i==(MAXF-1) c=' ';
                                   }
                              F[i]='\0';
                              tok->ttelem = FONE;
    /*                          strcpy(tok->tpselem, F);*/
                              tok->tpselem = *F;
                              if ((strstr(tok->tpselem,"-") == NULL) || (strlen(F)<8) || (strlen(F)>10))  {
                                printf ("\n Erro de inserçao do telefone!");
                                printf ("\n Insira novamente o numero do telefone no formato nnnn-nnnn\n ");
                                c = getche();
                                VerChFo(&c);
                              }
                              } while (( strstr(tok->tpselem,"-") == NULL ) || (strlen(F)<8) || (strlen(F)>10));
                          return tok;
                          }
                          else {
                              printf ("\n Erro de inserção!!! Insira o nome entre \"s e separe-o do telefone por espaço");
                              c=getche();
                            }
    
            }
            tok = NULL;
            return tok;
            }
    
    PLISTELEM LTEIns (void) {
            int TL = TOK;
            PLISTELEM LE;
            TOKELEM TN, TF;
    
            LE = malloc (sizeof(struct elemento));
            LE->telem = TL;
            fflush(stdin);
            while ( 1 ){
                TN = GetToken();
                if (TN->ttelem == NOME) {
                   LE->elem.toelem->nomelem = TN;
                   TF = GetToken();
                   while ( TF->ttelem != FONE ) {
                    printf ("\n Erro ao inserir o número do telefone!");
                    printf ("\n Insira o número do telefone no formato nnnn-nnnn:\n\n ");
                    TF = GetToken();
                   }
                   LE->elem.toelem->fonelem = TF;
                   return LE;
                   }
                   else
                       printf ("\nErro ao inserir o nome!!!");
                       printf ("Insira os dados no formato \"nome\" nnnn-nnnn: \n\n ");
            }
           }
    Thanks and sorry for so much code

    Frigga

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    /* strcpy(tok->tpselem, F);*/
    tok->tpselem = *F;
    I think you need to allocate memory for tpselem:
    Code:
    tok->tpselem = strdup(F);

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    Talking Thanks :o)

    thanks guys for your help!!!

    btw, strdup(F); hadn't worked at all (i had tried before) and don't ask me why :o)

    about the fflush(stdin); it's is just to get away the keyboard echo if the user type some function key :oP

    buy VvV thanks because the code you sent me worked :o)
    My brain was already melting and well, i should had thought i could allocate in that way instead of strdup(F); :o)

    and YOU are right, i'm using Turbo C 2.01 and I HATE IT!!!! :o/

    (But i need to use it since it's a homework for my college)

    thanks again guys i knew i had missed something :o)

Popular pages Recent additions subscribe to a feed