Expression: (unsigned)(c+1) <=256 Error

This is a discussion on Expression: (unsigned)(c+1) <=256 Error within the C Programming forums, part of the General Programming Boards category; I am writing some code that involves strings and everytime I compile it, I get the error: Expression: (unsigned)(c+1) <=256 ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    44

    Expression: (unsigned)(c+1) <=256 Error

    I am writing some code that involves strings and everytime I compile it, I get the error: Expression: (unsigned)(c+1) <=256 Error.

    I've done some looking around and found that it is a very generic error so I was hoping somebody could look at my code and point me in the right direction.

    I would post the picture of my error but I seem to be having trouble including addon's to this post.

    Any feedback would be great.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main (void)
    {
        char str[81];
        int length, i = 0;
        
        do 
        {
            
            printf ("Input string values: "); 
            while ((str[i] = getchar()) != '\n')
            {
                i++;
            }
            length = strlen(str);
    
            for (i = 0; i < length; i++)
            {
                if (isalpha(str[i]))
                    printf("%c is a letter\n",str[i]);
                if (isdigit(str[i]))
                    printf("%c is a digit\n",str[i]);
                if (isalnum(str[i]))
                    printf("%c is alphanumeric\n",str[i]);
                if (isxdigit(str[i]))
                    printf("%c is a hexademical digit (0-9, A-F)\n",str[i]);
                if (isspace(str[i]))
                    printf("ASCII value %i is a white space char\n",str[i]);
            }
                printf("\n");
        }
            while (strlen(str) > 0);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195
    you could better you looping by using if and else if statements checking for the type of character and also in you first while statement you can merge like this: while(str[i++] = getchar() != '\n');it'll be easier to read and bette hope it works...

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Thanks for the quick reply and the tip.

    I don't think I want to use else if statements because I want it to analyze each character in the string and decide which category it goes into even if the character goes into multiple categories.

    I still wasn't able to fix the error message though.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never actually terminate your "string" when you read it in, so it's not actually a string, so strlen is going to probably crash your program when you call it (because you don't actually have a string). I also don't see what cast you are talking about there in the error you are supposedly getting. You also never pay attention to how many characters you are reading in.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Looks like quzah's pointed out the big error, but I still don't see where the error you mention is coming from. What line is it on? What compiler are you using?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    getchar returns an int, so you would have to declare an int variable to hold it's result. It does this because it needs to be able to return every possible char value plus any special values like EOF to denote end of input. Something like:
    Code:
    int ch;
    char str[81];
    
    i = 0;
    while ((ch = getchar()) != EOF && ch != '\n' && i < sizeof(str)) {
        str[i++] = ch;
    }
    You could also use fgets to read in your string: fgets(str, sizeof(str), stdin); reads a whole line. It also null terminates your string and makes sure you don't overflow it. There is an example here: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

    EDIT: I suspect the error comes from your assigning the return value of getchar() to a char instead of an int, though it is a strange way of saying so. It sounds like getchar is a macro on your system. Are you sure you weren't using getc before, which may be implemented as a macro? Also, please provide the exact code, and the exact error message along with line number (i.e. copy-paste it, don't paraphrase).
    Last edited by anduril462; 03-29-2012 at 05:27 PM.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Ok here is what i have so far. I have made very minor changes to the code, just that now I broke up my sorting into different categories and I am counting how many characters are in each category instead of which category the character fits. I don't think those changes are too drastic.

    I have also updated my code with anduril462 suggestion.

    You could also use fgets to read in your string: fgets(str, sizeof(str), stdin); reads a whole line. It also null terminates your string and makes sure you don't overflow it
    The instructions I am following specifically says to use getchar() only.

    I am using Microsoft Visual C++ 2010 Express and the particular error I am receiving does not tell me which line it occurs on just a window that pops up after I enter in values for my sting.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main (void)
    {
        char str[81];
        int length, i = 0, count = 0, count_1 = 0,count_2 = 0, count_3 = 0, count_4 = 0, ch;
        
        do
        {
            printf ("Input string values: ");
            while ((ch = getchar()) != EOF && ch!= '\n' && i < sizeof(str))
            {
                str[i++] = ch;
            }
    
            length = strlen(str);
    
            for (i = 0; i < length; i++)
            {
                if (isalpha(str[i]))
                    count++;
                printf ("%i characters are alphabetic\n", count);
                if (islower(str[i]))
                    count_1++;
                printf ("%i characters are lowercase\n", count_1);
                if (isupper(str[i]))
                    count_2++;
                printf ("%i characters are uppercase\n", count_2);
                if (isdigit(str[i]))
                    count_3++;
                printf ("%i characters are numeric\n", count_3);
                if (isalpha(str[i]))
                    count_4++;
                printf ("%i characters are alphanumeric\n", count_4);
                printf("\n");
            }
        }
            while (strlen(str) > 0);
    
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by skmightymouse View Post
    Ok here is what i have so far. I have made very minor changes to the code, just that now I broke up my sorting into different categories and I am counting how many characters are in each category instead of which category the character fits. I don't think those changes are too drastic.

    I have also updated my code with anduril462 suggestion.

    The instructions I am following specifically says to use getchar() only.
    Fair enough. Looks a bit better, but you still don't null terminate your string like Quzah mentioned. There's actually a small error in my code, you need i < sizeof(str)-1, to leave room for the null terminator. When the input loop is done, simply do str[i] = '\0'; Then you will have a properly terminated string, and functions like strlen will work.

    Also, your count variables are horribly named. Make them reflect their purporse: count_alpha, count_lower, count_upper, count_num and count_alphanum. Then, realize that you're printing every time through the loop. That is not what you want. Move those printf statements out of the for loop. On line 34, count_alphanum (formerly count_4) is checking isalpha, which wont count digits. Lastly, you can use length instead of strlen(str) in your do-while loop on line 40.

    I am using Microsoft Visual C++ 2010 Express and the particular error I am receiving does not tell me which line it occurs on just a window that pops up after I enter in values for my sting.
    Sounds like you're running in debug mode and you're hitting an assert statement or something. Wherever that is happening (maybe in your isalpha type functions), again, it could be related to the whole garbage-data-unterminated-string stuff.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Thanks for all the feedback its been extremely helpful so far. I am always looking for ways to clean up my code and make it run more efficiently.

    Unfortunately I am receiving the same error but I was able to figure out how to attach the picture to this post so here it is Name:  Capture.PNG
Views: 1115
Size:  45.9 KB

    Here is the revised code, please let me know if I made the correct changes.

    Code:
    #define  _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main (void)
    {
        char str[81];
        int length, i = 0 , count_alpha = 0, count_lowercase = 0,
            count_uppercase = 0, count_digit = 0, count_alphanumeric = 0, ch;
        
        do
        {
            printf ("Input string values: ");
            str[i] = 0;
    
            while ((ch = getchar()) != EOF && ch!= '\n' && i < sizeof(str)-1)
            {
                str[i++] = ch;
            }
    
            length = strlen(str);
    
            for (i = 0; i < length; i++)
            {
                if (isalpha(str[i]))
                    count_alpha++;
                if (islower(str[i]))
                    count_lowercase++;
                if (isupper(str[i]))
                    count_uppercase++;
                if (isdigit(str[i]))
                    count_digit++;
                if (isalnum(str[i]))
                    count_alphanumeric++;
            }
                printf ("%i characters are alphabetic\n", count_alpha);
                printf ("%i characters are lowercase\n", count_lowercase);
                printf ("%i characters are uppercase\n", count_uppercase);
                printf ("%i characters are numeric\n", count_digit);
                printf ("%i characters are alphanumeric\n", count_alphanumeric);
                printf("\n");
        }
            while (length > 0);
    
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    isctype.c sounds like one of the files from the standard library, that has the isalpha et al. funcitons in them. I'm still convinced it's from an un-terminated string. You're terminating your string before you have anything in it, then copying over the null terminator with user input. Move line 15 down to line 21 (after the input loop). Also, you should initialize i and all your count variables to 0 at the very beginning of the do-while loop, basically at line 14. That way, if they decide to "play again", the counts wont be wacky.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Thank you so much, you were correct that I was not ending my string correctly.

    I appreciate all your help!

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    You should always zero out your strings before you start to use them and every time you need to reset them to a starting condition:

    char char_array[ARRAYSIZE];

    memset( char_array, 0, ARRAYSIZE);

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Cynic
    You should always zero out your strings before you start to use them
    The way I see it, if you want to do that, then you might as well write:
    Code:
    char char_array[ARRAYSIZE] = "";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: expected an expression
    By cdmstr in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2011, 02:00 PM
  2. DWARF-2 expression error
    By programhelp in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2010, 07:18 PM
  3. Replies: 2
    Last Post: 11-25-2009, 07:38 AM
  4. Compiler Error: Unsigned vs Signed Integer Expression
    By ChristianTool in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2005, 05:57 PM
  5. expression error?
    By trekker in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2003, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21