Hey everyone,

I have a code here with me and I'm having trouble putting my conditions.
I'm basically putting a string and I have to split it into 4 and atoi to change a char to an int but basically when I run the program failed.
It tells me "Ordered comparison between pointer to integer (char to int)"

Have a look at it. The problem is my condition I put.

void check_word(char word);
void check_year(int year);
void check_definition(char definition);
void check_synonyms(char synonym);


//------------------------------------------------------------------//
//                          SPLITTING THE STRING
//------------------------------------------------------------------//

char *split(char words[100])
{
    int i = 0;
    char* words_dup = malloc(strlen(words)+1);
    strcpy(words_dup,words);

    while (words!='\0')
    {
        char *word=strtok(words, "_#_");
        check_word(*word);

        char *year=strtok(NULL, "_#_");;  // assigning NULL for previousely where it left off
        i=atoi(year);
        check_year(i);

        char *definition=strtok(NULL,"_#_");
        check_definition(*definition);

        char *synonyms=strtok(NULL,"_#_");
        check_synonyms(*synonyms);


        printf("%s\t", word);
        printf("%i\t",i);
        printf("%s\t", definition);
        printf("%s\t", synonyms);
    }

    // now restore words
    strcpy(words,words_dup);
    free(words_dup);
    return 0;
}

//------------------------------------------------------------------//
//                     CHECKING LEGAL OR NOT
//------------------------------------------------------------------//
void check_word(char word)
{
    if (word>='A' && word<='Z')
    {
        printf("not legal\n");
    }
}


void check_year(int year)
{
    if (year<0)
    {
        printf("not legal\n");
    }
}

void check_definition(char definition)
{
    if (definition>='A' && definition<='Z')
    {
        printf("not legal\n");
    }
}

void check_synonyms(char synonym)
{
    if (synonym>='a' && synonym<='z')
    {
        printf("not legal\n");
    }
}


int main()
{
    char words[100];
    printf("Enter a string\n");
    scanf("%s", words);
    split(words);
}

The string that I'm trying to input is:

hello_#2003#my#_Name

Any help?

Recommended Answers

All 7 Replies

Is this the same program you posted in the C forum?

It tells me "Ordered comparison between pointer to integer (char to int)"

Which line does that error message belong to? Your compiler should have given you the line number.

not exactly but i'm having a problem with atoi. I know i have to check for the pointer to be NULL but since i'm new to C i have no idea how i tried doing it. I know i'm asking much but can you give me an example where i need to set a pointer to NULL in my case?

Here is how to check if a pointer is not NULL before using it:

if (pointer != NULL)
{
  // do something with the pointer
}
else
{
  // don't do something with the pointer
}

What makes you think it doesn't work? Is there, perhaps, some kind of error message you could tell us?

The problem is your while loop. You go round the loop too many times.

This code fixes the problem by removing the while loop. If you need to process more than one set of data, you'll need a loop that works.

void check_word(char word);
void check_year(int year);
void check_definition(char definition);
void check_synonyms(char synonym);
//------------------------------------------------------------------//
//                          SPLITTING THE STRING
//------------------------------------------------------------------//
char *split(char words[100])
{
    int i = 0;
    char* words_dup = malloc(strlen(words)+1);
    strcpy(words_dup,words);

        char *word=strtok(words, "_#_");
        check_word(*word);
        char *year=strtok(NULL, "_#_");;  // assigning NULL for previousely where it left off
        i=atoi(year);
        check_year(i);
        char *definition=strtok(NULL,"_#_");
        check_definition(*definition);
        char *synonyms=strtok(NULL,"_#_");
        check_synonyms(*synonyms);
        printf("%s\t", word);
        printf("%i\t",i);
        printf("%s\t", definition);
        printf("%s\t", synonyms);

    // now restore words
    strcpy(words,words_dup);
    free(words_dup);
    return 0;
}
//------------------------------------------------------------------//
//                     CHECKING LEGAL OR NOT
//------------------------------------------------------------------//
void check_word(char word)
{
    if (word>='A' && word<='Z')
    {
        printf("not legal\n");
    }
}
void check_year(int year)
{
    if (year<0)
    {
        printf("not legal\n");
    }
}
void check_definition(char definition)
{
    if (definition>='A' && definition<='Z')
    {
        printf("not legal\n");
    }
}
void check_synonyms(char synonym)
{
    if (synonym>='a' && synonym<='z')
    {
        printf("not legal\n");
    }
}
int main()
{
    char words[100];
    printf("Enter a string\n");
    scanf("%s", words);
    split(words);
}

okay the reason why i put a while loop is simply because i want to avoid the input: hello_#_ should i restore it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.