Validate a user input integer?

This is a discussion on Validate a user input integer? within the C++ Programming forums, part of the General Programming Boards category; I have a project that I am working on that requires me to validate an integer. here's the specifications: Valid ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Validate a user input integer?

    I have a project that I am working on that requires me to validate an integer. here's the specifications:

    Valid ISBN numbers are exactly 5 digts long. The low order digit (called the check digit) is the sum of the four high-order digts modulus 9. If the ISBN is invalid, display an appropriate message and return to the main menu. Remember that the ISBN is entered as an integer not seperated digits...

    First things first... how do I go about doing this? I have a very limited C++ background, this project is for a first year course... Can anyone point me somewhere that could explain how to do this, as I would like to write as much of the code on my own as possible, but I am running out of time and do not have the time to search on my own...

    Thank you in advance!!

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    for the 5 digit check I "think" (Im pretty sure there is some other better way) will work:
    Code:
    if(number<100000 && number > 9999)
        //Code here

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    alright, that is good enough for what I need for sure, but how do you "seperate" the digits?? That's where I am kinda lost...

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I would probably take the input as a string, and you get access to the length (str.length()), each digit (str[n]), and you could always convert it to an integer (i = atoi(str)).
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    To separate digits in an integer, use the divide and modulus operators. This might be even easier than string conversions (if you already have an integer to start with).
    Code:
    int i = 12345;
    int digit1 = i / 10000;
    int digit2 = (i / 1000) % 10;
    int digit3 = (i / 100) % 10;
    int digit4 = (i / 10) % 10;
    int digit5 = i % 10;

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I would probably take the input as a string
    It isn't any more difficult to parse an integer in such a simple way. Besides, you can avoid the string to integer conversions if you do it something like this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    bool valid ( int isbn )
    {
      int n;
      int save = isbn % 10;
      int sum = 0;
    
      for ( n = 1; ( isbn /= 10 ) != 0; n++ )
        sum += isbn % 10;
      if ( n != 5 || save % 9 != sum )
        return false;
      
      return true;
    }
    
    int main()
    {
      int input;
    
      cout<<"Enter ISBN: ";
      if ( valid ( input ) )
        cout<<"Valid ISBN"<<endl;
    }
    My best code is written with the delete key.

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    > This might be even easier than string conversions (if you already have an integer to start with).
    Dude, how hard is it to convert an int to a string, and the opposite than using the function atoi() and itoa()?? You could utilize stringstream also, actually.
    Anyway, my solution was inspired by this article entitled Using String-based Data Validation
    Prelude might offer an elegant solution, but my head upside down trying to understand what's happening during the validation process.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The main reason you would want to use string here is that it's the only way to keep leading zero's. 00112 is a valid ISBN in this scheme, while 112 is not. gcount is only updated for the "raw" operations like getline(). Real ISBN's also have multiple int's seperated by '-'. Not that this makes a bit of difference here.

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >The main reason you would want to use string here is that it's the only way to keep leading zero's.
    Good point, should've said that
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Dude, how hard is it to convert an int to a string
    Not very, but doing it efficiently, safely, and portably is not as easy as you would think.

    >and the opposite than using the function atoi() and itoa()??
    This makes my case, really. atoi is unsafe and itoa is nonportable. As you say you can use stringstream, but then you're creating a whole new object (even if it is highly optimized) when you could be working directly with the integer with less effort in this case. Then of course there is the part about having to call a string to integer converstion function to add to the sum for every digit. Once you understand the idiom of processing an integer one digit at a time, it is trivial to follow.

    Of course, after that little rant, I notice that another restriction has been added to the function in that it now must allow leading zeros. If OPs were considerate enough to give us appropriate details I wouldn't make a fool out of myself...
    My best code is written with the delete key.

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Not saying that using a string here is a bad idea, it really depends on what is required. In the simple case of being given an integer and printing out yes or no this is valid, I still think using / and % is easier, especially if the programmer is a beginner who isn't used to atoi, stringstreams, etc.

    The leading zeros restriction was not added by the OP, and it doesn't matter anyway if you are just doing data validation. The div and mod validation technique works fine with 00112. It does make it a little harder to format the output correctly, but that is assuming the data needs to be spit back out.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The div and mod validation technique works fine with 00112.
    Not if you're required to count the digits as well. Leading zeros work, but not correctly because the leading zeros are truncated. This changes the number of digits and validation will fail.
    My best code is written with the delete key.

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by Prelude
    Not if you're required to count the digits as well. Leading zeros work, but not correctly because the leading zeros are truncated. This changes the number of digits and validation will fail.
    I guess it just depends on what you assume. Since the OP wanted to validate an integer, and since it is not possible to tell the difference between 112 and 00112 if both are stored as integers, then some assumptions need to be made to resolve that.

    If you assume that the number is read in from an input, then yes, reading it in as a string is necessary to validate the number of digits. If you assume that any integer with fewer than 5 digits should have leading 0's, then the strictly integer data validation works. By the way, I think I spotted a small error in your code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    bool valid ( int isbn )
    {
      int n;
      int save = isbn % 10;
      int sum = 0;
    
      for ( n = 1; ( isbn /= 10 ) != 0; n++ )
        sum += isbn % 10;
      if ( n > 5 || sum % 9 != save )  // Switched sum and save. (also changed digit check to allow missing leading 0's)
        return false;
      
      return true;
    }
    
    int main()
    {
      if ( valid (112) )
        cout<<"112 (00112) is a Valid ISBN"<<endl;
      if ( valid (12341) )
        cout<<"12341 is a Valid ISBN"<<endl;
    }

  14. #14
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    Thanks for all the help guys, I just wanted the simplest way to take care of this... didn't mean to start an entire war on whether or not to use int or string... Thanks again, I really appreciate this, it saved me alot of time...

  15. #15
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    No, problem. We enjoyed it. That's how we learn from one another. Hope you learn something, too.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM

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