Thread: NOOOOB!!!! - Tokenizing code help

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    Post NOOOOB!!!! - Tokenizing code help

    Hey guys I'm new to this stuff so bare with me.....
    I've been working with this code for a little while now and cant seem to get past this segmentation fault. I'm just trying to take user input (in the form of a spaced equation like 2 + 2) and tokenize it, then place the result in an additional array. At the very end of the do-while loop I get a segmentation fault. The debugger tells me that its in a strcmp() method somewhere but I cant seem to figure it out.
    All the help is greatly appretiated

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    #define MAXLINE 132
    #define EXP '^'
    #define LEFT_BRAKET '('
    #define RIGHT_BRAKET ')'
    #define DEVIDE '/'
    #define MULT '*'
    #define ADD '+'
    #define SUBTRACT '-'
    
    char ch[MAXLINE][MAXLINE];
    char copych[MAXLINE][MAXLINE];
    
    int main(int argc, char *argv[]){
       int i = 0;
    int kill_the_SF = 0;
       int length, lengthb, length_counter = 1;
       char test_string[MAXLINE];
       char *token;
       char text_stringvtwo[MAXLINE][MAXLINE];
            int result = 0;
    
    
    
    
    
    
    
            if (argc <3){
                    printf("no Argument time to toke!\n");
                    printf("welcome to the very basic C calculator\n Please enter an equation:");
    
                    fgets(ch,MAXLINE,stdin);
    
                    printf("\nInput = %s", ch);
    
    
                    strcpy(test_string,ch);
    
    
                    printf("\nCopy = %s", test_string);
    
                    lengthb = strlen(test_string);
    
    
                    token = NULL;
                    token = strtok(test_string, " ");
    
    
    
                    printf("\n+++++++++++++++++++++++++++++++++token = [ %s ] @ %d++++++++++++++++++++++++++++++++++\n\n\n", token, i);
                                                                                                                                                               strcpy(text_stringvtwo[i++], token);
    
                    length = strlen(token);
    
                    length_counter = length_counter + length + 1;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
            do {
    
    
    
    
                                    token = strtok(NULL, " ");
                                    printf("In while loop");
    
                                    if (token != NULL ){
    
    
                                            if (strcmp(text_stringvtwo[i], "\n")){
                                                    strncpy(text_stringvtwo[i], token, length-1);
                                            }
    
                                            strcpy(text_stringvtwo[i], token  );
                                            printf("\n++++++++++++++++++++++++token =[ %s ] @ %d+++++++++++++++++++++++++\n\n\n", token, i);
    
                                            printf("lengths:\nCurrent token:\t%d\nCurrent String:\t%dnOriginal String:\t%d\n",length ,strlen(text_stringvtwo) ,lengthb);
    
                                           
     printf("Length counter =\t%d\nIndex =\t%d\n", length_counter, i);
                                           
     printf("Values:\n\tString val:\t%s\n\tToken val:\t%s\n",text_stringvtwo[i], token);
    
    
    
    
    
                                    }
    
    
                                    length_counter = length_counter + strlen(token );
                                    i++;
    
                    }while (token != NULL && kill_the_SF ==0);
    
    
    
    
                    printf("i made it out!");
    
    
    
            }
    
            else{
                    printf ("condition has been met");
                    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
            while ( strcmp(text_stringvtwo[i], NULL ) ){
                                    printf("I'm in a while loop");
                                    if (strlen(token) == 1){
    
    
                                    switch (token[0]) {
    
                                            case EXP:
                                                    printf("this program does not support exponential equations");
                                                    break;
    
                                            case LEFT_BRAKET:
                                                    printf("left b");
                                                    break;
    
                                            case RIGHT_BRAKET:
                                                    printf("right b");
                                                    break;
    
                                            case DEVIDE:
                                                    printf("devistion");
                                                    break;
    
                                            case MULT:
                                                    printf("multiplication");
                                                    break;
    
                                            case ADD:
                                                    printf("addition");
                                                    break;
    
                                            case SUBTRACT:
                                                    printf("subtract");
                                                    break;
    
                                            default:
                                                    printf("default");
                                            break;
    
                                    }
    
                            }
                            i++;
                    }
                    printf("I mane it out");
                    i = 1;
                    for (i; i < argc; i++){
                            strcpy(copych[i-1], argv[i]);
                            printf("%s", copych[i-1]);
                    }
    
    }
    Sorry bout the large printlines i tried to make them ledgeble.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are LOTS of suspicious bits of code here:

    strtok MAY return NULL, and whilst printf() may cope with that by printing "(NULL)" instead of the string, I'm pretty sure that strlen() isn't so obliging.

    You are also reading data into a 2D array, which is probably not quite what you should be doing.

    Putting the newline at the END of your printf rather than at the beginning improves the chances of the output actually being printed before the NEXT printf - so if you are using that to identify where your code is getting to, then it would certainly help. Adding an fflush(stdout) after each printf, or using fprintf(stderr, ...) instead of printf, is even more "safe", as it ensures that the data is written to the console before moving on to the next line of code.

    Code:
     strcmp(text_stringvtwo[i], NULL ) ){
    will not EVER work - you are comparing a string with the string at address NULL - which will lead to a segfault if you get that far in the first place. What are you ACTUALLY trying to do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4
    lol dont even remember what i was thinkng with that line :P, but the purpose of the switch statement is to id operators in the string and preform resultent code after. I'm most likely going to try and change that to a for loop.

    Thanks alot for the tips, ill start trying them immediately

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4
    So I tried all the tips and im still getting a seg error, only this time its at the end of the code.
    I've put a comment after the last output i see b4 the seg fault.

    Here's the updated code
    Code:
    ;
    
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    #define MAXLINE 132
    #define EXP '^'
    #define LEFT_BRAKET '('
    #define RIGHT_BRAKET ')'
    #define DEVIDE '/'
    #define MULT '*'
    #define ADD '+'
    #define SUBTRACT '-'
    
    char ch[MAXLINE];
    
    int main(int argc, char *argv[]){
       int i = 0;
       int kill_the_SF = 0;
       int length, lengthb, length_counter = 1;
       char test_string[MAXLINE];
       char *token;
       char text_stringvtwo[MAXLINE];
       char text_stringthree[MAXLINE];
       int result = 0;
    
    
    
    
    
    
    
            if (argc <3){
                    printf("no Argument time to toke!\n");
                    printf("welcome to the very basic C calculator\n Please enter an equation:");
    
                    fgets(ch,MAXLINE,stdin);
    
                    printf("Input = %s\n", ch);
                    fflush(stdin);
    
                    strcpy(test_string,ch);
    
    
                    printf("Copy = %s\n", test_string);
                    fflush(stdin);
    
                    lengthb = strlen(test_string);
    
    
    
                    token = strtok(test_string, " ");
    
    
    
    
    
    
                    printf("+++++++++++++++++++++++++++++++++token = [ %s ] @ %d++++++++++++++++\n\n\n", token, i);
                    fflush(stdin);
    
                    strcpy(text_stringvtwo, token);
                    i++;
    
                    if (token!=NULL){
                            printf("Im NOT NULL\n");
                            fflush(stdin);
                            length = strlen(token);
                            length_counter =+ length+1;
                    }
                    else
                            length_counter = 0;
                    printf("\nlength_counter = %d",length_counter);
    
                    i =1;
    
    
    
    
    
    
    
    
    
    
    
    
    
            do{
    
    
    
    
                                    token = strtok(NULL, " ");
    
                                    printf("In while loop\n");
                                    fflush(stdin);//at this point I get a seg Fault
    
                                    if (token != NULL ){
    
                                            printf("im in another while loop\n");
                                            fflush(stdin);
                                            strcpy(text_stringvtwo, token);
    
                                            if (strcmp(token, "\\n") == 0){
                                                    printf("it is equil to \\n");
                                                    fflush(stdin);
                                                    strncpy(text_stringvtwo, token, length-1);
                                            }
                                            printf("\n++++++++++++++++++++token =[ %s ] @ %d++++++++++++++++++\n\n\n", token, i);
                                            fflush(stdin);
                                            printf("lengths:\nCurrent token:\t%d\nCurrent String:\t%d\nOriginal String:\t%d\n",length ,strlen(text_stringvtwo) ,lengthb);
                                            printf("Length counter =\t%d\nIndex =\t%d\n", length_counter, i);
                                            printf("Values:\n\tToken val:\t%s\n",token);
    
    
    
    
    
                                    }
                                    else
                                            break;
    
    
    
    
                    }while (token != NULL);
                    token = NULL;
    
    
    
                    printf("i made it out!");
    
    
    
            }
    
            else{
                    printf ("condition has been met");
                    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
            while ( strcmp(text_stringvtwo[i], NULL ) ){//I know its the same, im working on the above 1st, code doesn't make it here anyways
                                    printf("I'm in a while loop");
                                    if (strlen(token) == 1){
    
    
                                    switch (token[0]) {
    
                                            case EXP:
                                                    printf("this program does not support exponential equations");
                                                                                                                                               163,1-8       73%
                                                    break;
    
                                            case LEFT_BRAKET:
                                                    printf("left b");
                                                    break;
    
                                            case RIGHT_BRAKET:
                                                    printf("right b");
                                                    break;
    
                                            case DEVIDE:
                                                    printf("devistion");
                                                    break;
    
                                            case MULT:
                                                    printf("multiplication");
                                                    break;
    
                                            case ADD:
                                                    printf("addition");
                                                    break;
    
                                            case SUBTRACT:
                                                    printf("subtract");
                                                    break;
    
                                            default:
                                                    printf("default");
                                            break;
    
                                    }
    
                            }
                            i++;
                    }
                    printf("I mane it out");
    
    }
                                                                                                                                               201,1         Bot

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4
    <ignore the ; at the top - typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM