How to verify length properly?

Okay so I'm trying to verify a PIN number and Zipcode. The PIN should be 4 digits long and the Zipcode should be 5 digits. How can I do this using Int numbers only? I tried to but some error came up. I settled for string, it works, but obviously it allows letters in addition to numbers. I only want numbers (integers). Any help would be appreciated. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
using namespace std;

bool is4Digits(string); // function prototype
bool ZipCode(string);

int main()
{
	string zipcode;
	string PIN;
	cout << "Enter a 4 digit pin." << endl;
	cin >> PIN;

	if (is4Digits(PIN) == 1) //The "1" is stating if it's "true" then the PIN will be accepted. Checking if the PIN is 4 digits.
		cout << "PIN accepted." << endl;
	else 
		cout << "PIN denied." << endl;

	cout << "Please enter your 5 digit zipcode." << endl;
	cin >> zipcode;

	if(ZipCode(zipcode) == 1)
		cout << "Valid 5 digit zipcode." << endl;
	else 
		cout << "Invalid zipcode." << endl;

	system("pause");
	return 0;
}

bool ZipCode (string zip)
{
	bool verify = true;

	if (zip.length() != 5)
	{
		verify = false;
	}
	return verify;
}

bool is4Digits (string pin)
{
	bool status = true;

	if (pin.length() != 4)
	{
		status = false;
	}
	return status;
}
I would stick with the string and use one of the string functions to determine if there is some other character other than a digit, something like string.find_first_of() possibly.

You can do it with integers, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool is4Digits(int zip)
{
      numDigits = 0;
      while(zip > 0)
      {
           zip /= 10; 
           numDigits++;
      }
      
      if(numDigits == 4)
            return true;
      else 
            return false;
}
Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cctype>
bool is4Digits (string pin)
{
  if (pin.length() != 4)
  {
     return false;
  }
  for (size_t i = 0; i < pin.length(); i++)
  {
     if (!isdigit(pin[i]))
     {
         return false;
     }
  }	
  return true;
}
Alternative you could use regular expressions - though it might be a bit of an overkill for such a simple task.

Thomas1965,

Thank you, that actually works nicely. Could you explain this, how this exactly works?
You can do it with integers, like so:

No you can't. Integers don't preserve leading zeros, which can occur in both a pin and a simple zip code.

Why not just use the string.find() functions. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
    std::string pin = "a234";
    if(pin.length() != 4 )
        std::cerr << "Error improper number of characters in the pin (must be 4 characters long)!\n";
    else if(pin.find_first_not_of("0123456789") != std::string::npos)
        std::cerr << "Error pin must only contain digits.\n";
    else
        std::cerr << "Pin only contains digits and is 4 digits long.\n" ;
    return 0;
}


No you can't. Integers don't preserve leading zeros, which can occur in both a pin and a simple zip code.


I stand corrected.
closed account (48T7M4Gy)
For an explanation of thomas's code, read it and even better than that run through it line by line with some examples. You can find out about isdigit by googling it or looking up the reference section of this site. Same goes for any other stuff that's not clear.
Topic archived. No new replies allowed.