Like Tree3Likes
  • 1 Post By std10093
  • 1 Post By Matticus
  • 1 Post By jwroblewski44

String not validating properly.

This is a discussion on String not validating properly. within the C Programming forums, part of the General Programming Boards category; Hi all. First time poster and beginner in programming. I'm having problems with my program seen below. It compiles and ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    String not validating properly.

    Hi all. First time poster and beginner in programming. I'm having problems with my program seen below. It compiles and will run but I have the following 3 problems.

    1. In main() in the while loop entering 'q' does not exit.

    2. in the isbnValidation function. The size check for minimum length words will work, but the check for "Invalid ISBN character" doesn't.

    3. Every ISBN entered returns as valid. The formula is supposed to multiply the first digit by 10, next by 9 etc (skipping all the dashes) and add up to a weighted total which if valid will divide evenly by 11.

    I'm really new to this and I'm finding arrays and strings quite challenging. Any help or advise anyone is willing to give I would really appreciate.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define ISBNMIN 10
    #define ISBNMAX 14
    
    
    
    
    void isbnValidation(char *p_isbn, char *p_orig);
    
    
    int main(void)
    {
        char isbn[ISBNMAX];
        char orig[ISBNMAX];
            
        printf("Enter an ISBN [q to exit]: ");    
        
        while(scanf("%s", isbn) != 'q')
        {
            strcpy(orig, isbn);        
            
            isbnValidation(isbn, orig);
    
    
            printf("Enter an ISBN [q to exit]: ");
        }
        return 0;
    }
    
    
        
    void isbnValidation(char *p_isbn, char *p_orig)
    {
        int i, digit;
        int total = 0;
        int weight = 10;
    
    
        if(strlen(p_isbn) < ISBNMIN)
        {
            printf("ISBN must be minimum 10 digits\n");
            return;
        }
    
    
        for(i = 0; i < strlen(p_isbn-1); i++)
        {
            if(p_isbn[i] < '0' || p_isbn[i] > '9' || p_isbn[i] != '-' || p_isbn[i] != '\0')
            {
                if(toupper(p_isbn[i]) != 'X')
                {            
                    printf("Invalid ISBN character\n");
                    return;
                }
            }
        }    
    
    
        for(i = 0; i < strlen(p_isbn-1); i++, weight--)
        {
            if(p_isbn[i] != '-')
            {        
                if(p_isbn[i] == 'x' || p_isbn[i] == 'X')
                    digit = 10;
                else        
                    digit = p_isbn[i];        
            
                total += digit * weight;
    
    
            }
        }
    
    
        if(total % 11 == 0)
        {
            printf("ISBN %s is valid.\n", p_orig);
        }
        else
        {
            printf("ISBN %s is not valid.\n", p_orig);
        }
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    About scanf. Return value: On success, the function returns the number of items of the argument list successfully filled. Ok, so should check the isbn. If you don't succeed in first try, try to use strcmp.
    ScotchandTweed likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    1. Line 22:

    - "scanf()" does not return what it reads - it returns the number of items successfully read.
    - A string* cannot equal a single character (unless it's an empty string).
    - Strings cannot be compared using ==/!=, you must use "strcmp()" for that.

    *Note: A string must be terminated with a null character ('\0')

    2. If you're expecting a string of 10 characters, you need to give the character array enough space for 11 characters (see note above).

    Also, your logic is self-defeating.

    Code:
    // invalid check
    
    if(p_isbn[i] < '0' || p_isbn[i] > '9' || p_isbn[i] != '-' || p_isbn[i] != '\0')
    According to your logic, it's invalid if: It's less than '0' OR greater than '9' OR not a hyphen ... that is simply not possible. Name a character that is between '0' and '9' and is a hyphen at the same time.

    Not to mention that it's still not valid if it's somehow also not equal to 'X'.

    3. I didn't even get this far...
    ScotchandTweed likes this.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Code:
    while(scanf("%s", isbn) != 'q')    {
            strcpy(orig, isbn);
    This is dangerous. strcpy() will copy over any size string into the destination array, even if if is too large too fit. Use strncpy(). And as others have noted, scanf() returns the number of items read, the actual items read.
    Last edited by jwroblewski44; 03-06-2013 at 01:16 AM.
    ScotchandTweed likes this.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Thank you all for the info. It's slow going but I think I'm starting to get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with validating an input.
    By morrjame in forum C Programming
    Replies: 9
    Last Post: 12-20-2010, 09:55 PM
  2. why isnt this checking my string properly
    By Mythic Fr0st in forum C++ Programming
    Replies: 10
    Last Post: 01-17-2007, 11:07 PM
  3. Validating XML in ANSI C...
    By frozen in forum C Programming
    Replies: 2
    Last Post: 03-24-2006, 01:00 PM
  4. Can someone help me with validating?
    By Maria Jordan in forum C Programming
    Replies: 5
    Last Post: 03-27-2005, 04:41 AM
  5. Validating string for integers
    By mlsbbe in forum C Programming
    Replies: 4
    Last Post: 04-03-2003, 02:44 PM

Tags for this Thread


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