fgets error message

This is a discussion on fgets error message within the C Programming forums, part of the General Programming Boards category; I am using the code below as part of a bigger program to accept strings of words from the user ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    15

    fgets error message

    I am using the code below as part of a bigger program to accept strings of words from the user and analyze them using several other cases. I initially used gets where the program worked as it should but was warned about buffer overflow so am now trying to use fgets. The problem I have below is that I cant get it to display a separate error code for too many characters, it seems too jump to the default error code for the whole switch and ignore the printf in this case. Does anyone know how to get an error here to display and send it back to the options.


    Code:
    case 1:
                string[MAXSIZE];
    
    
                printf("\nEnter a string of words:\n\n");
                fgets (string, MAXSIZE, stdin);
                len = strlen(string);
                if (string[len-1] == '\n')
                    string[--len] = 0;
                if (string == MAXSIZE)
                    printf("Too many characters please try again");
                break;
    Last edited by johnpr; 03-06-2012 at 03:38 PM. Reason: Forgot code markers

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You can't declare variables right after a "case" statement. Not to mention that your variable declaration is wrong.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    Ok, I have removed the variable from the case, it was an attempt to clear the array for re-use. I have had numerous attempts to get this to display the error message but no luck, just running out of ideas now and my C books don't seem to handle this kind of error.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Huh? Post your current code, including the declaration of the array.

    Also, you might want to clear arrays with memset.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    Heres the complete program. It does work how I need it to with gets, just wanted to address the buffer overflow issue (Have kept array very small to make it easier to test)

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    int main(void)
    {
        const int MAXSIZE = 5;
        char string[MAXSIZE], b[5], c;
        int   len, i = 0, words = 1, function = 0, characters = 0, vow = 0, cons = 0, spaces = 0, dig = 0, other = 0;
    //
    //Variables declared above and menu options are below.
    //
        printf("Please choose option 1 initially to get started\n\n\n");
        while (function != 6)
        {
            function = 0;
            printf("\n1. Enter a words or a sentence\n");
            printf("2. Find word count\n");
            printf("3. Find character count\n");
            printf("4. Find the number of vowels and consonants\n");
            printf("5. Find if the string is a palindrome\n");
            printf("6. Quit\n\n");
            scanf("%d", &function);
            getchar();                    
            switch (function)
            {
    //
    //The options chosen above now select from the list of cases
    //
            case 1:            
                printf("\nEnter a string of words:\n\n");
                fgets (string, MAXSIZE, stdin);
                len = strlen(string);
                if (string[len-1] == '\n')
                    string[--len] =0;
                if (string==MAXSIZE)
                    printf("Too many characters please try again");
                break;
            case 2:    // This case counts the number of words in the string by counting spaces //
                words = 1;
                for (i = 0; i < strlen(string); i++)
                    if (string[i] == ' ')
                        words++;
                printf("\nThe word count is %d\n\n", words);
                break;
            case 3:// This case counts the number of alphabet characters only //
                characters = 0;
                for (i = 0; i < strlen(string); i++)
                    if (isalpha(string[i]))
                        characters++;
                printf("\nThe character count is %d\n\n", characters);
                break;
            case 4:// This case converts all characters to lower case the analyses it //
                vow = 0;
                cons = 0;
                dig = 0;
                spaces = 0;
                other = 0;
                i = 0;
                while ((c = tolower(string[i++])) != '\0')
                {
                    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
                        ++vow;
                    else if (c >= 'a' && c <= 'z')
                        ++cons;
                    else if (c >= '0' && c <= '9')
                        ++dig;
                    else if (c == ' ')
                    {
                        ++spaces;
                        while (string[i] == ' ' || string[i] == '\t')
                        {
                            i++;
                            spaces++;
                        }
                    }
                    else
                        other++;
                }
                printf("\nThe total number of vowels is %d\n", vow);
                printf("The total number of consonants is %d\n", cons);
                printf("The total number of numeric characters is %d\n", dig);
                printf("The total number of special characters is %d\n", other);
                printf("The total number of line spaces is %d\n\n", spaces);
                break;
            case 5: // This case tests for reversable text //
                strcpy(b, string);
                strrev(b);
                if (strcmp(string, b) == 0)
                    printf("\nThe string is a palindrome\n\n");
                else
                    printf("\nThe string is not a palindrome\n\n");
                break;
            case 6:
                break;
            default:
                printf("\nError please enter only between 1 and 5\n");
                break;
            }
        }
        return 0;
    }

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This refers to your original code.

    If you want to put a variable definition in a case clause you need braces to create a block. Also, your expression string == MAXSIZE is presumably supposed to be len == MAXSIZE-1, i.e., you mean to test len not string, and since it will never be MAXSIZE (it has to leave room for the 0), you mean to compare to MAXSIZE-1.
    Code:
    case 1: {
                string[MAXSIZE];
    
                printf("\nEnter a string of words:\n\n");
                fgets (string, MAXSIZE, stdin);
                len = strlen(string);
                if (string[len-1] == '\n')
                    string[--len] = 0;
                if (len == MAXSIZE - 1)
                    printf("Too many characters please try again");
                break;
            }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    The problem seems to be right here:

    Code:
    if (string==MAXSIZE)
                    printf("Too many characters please try again");
    The correct way would be to compare strlen(string) with MAXSIZE, but it's a useless test. By definition, fgets() won't read over the limit, and it will never be equal to or over MAXSIZE.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    The reason I wanted to including this was because if I do enter too many characters the program just throw's me out to the default error code at the end. Wanted to be aware that I had made a mistake within the case.

    I have tried both methods as above but both still have the same result, am I best just to leave this as an option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 10-08-2010, 06:35 PM
  2. Why Do I keep Getting this Error Message?
    By Nothing595 in forum C Programming
    Replies: 10
    Last Post: 09-25-2010, 04:24 PM
  3. fopen error & Assertion error - fgets.c
    By andyscouse78 in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2007, 04:33 AM
  4. fgets Error
    By Epo in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2003, 09:44 PM
  5. fgets error handling
    By mlsbbe in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 02:45 PM

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