Validate a user input integer?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-05-2003
criticalerror
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...

• 12-05-2003
Shakti
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```
• 12-05-2003
criticalerror
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...
• 12-05-2003
alphaoide
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)).
• 12-05-2003
jlou
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;```
• 12-05-2003
Prelude
>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; }```
• 12-05-2003
alphaoide
> 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.
• 12-05-2003
grib
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.
• 12-05-2003
alphaoide
>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
• 12-05-2003
Prelude
>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...:mad:
• 12-05-2003
jlou
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-05-2003
Prelude
>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.
• 12-05-2003
jlou
Quote:

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; }```
• 12-05-2003
criticalerror
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...
• 12-05-2003
alphaoide
No, problem. We enjoyed it. That's how we learn from one another. Hope you learn something, too.:)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last