Validating phone numbers

Say I have an array,

1
2
3
	long long phoneNumbers[] = { 1416123456LL, 14161234567LL, 1416234567890LL,
		14162345678LL, -1LL, 124163456789LL, 
		14161230002LL };


And I need to validate that the first and second digits of each phone number are non-zero, that each phone number is 12 digits long, and then store them into an array in a class instance.

ie, when I do Contact someContact("John Doe", phoneNumbers, 7);, it'll create an instance someContact, and store ONLY VALID phone numbers in that instance.

My class is
1
2
3
4
5
6
7
8
9
	class Contact {
		char name[20];
		int* m_pNumber;
		short amtNumbers;
	public:
		Contact();
		Contact(const char* tempName, const int* tempNumber, short amount);
		~Contact();
                int validNumbers(const int* array, int count);


My default constructor should do this. I have defined my ctor as,
1
2
3
4
5
6
7
8
9
10
	Contact::Contact(const char* tempName, const int* tempNumber, short amount) {
		for (int i = 0; i < 20; i++) {
			name[i] = tempName[i];
			}
			int valids = validNumbers(tempNumber, amount);
			m_pNumber = new int[valids];
			for (int i = 0; i < valids; i++) {
				m_pNumber[i] = tempNumber[i];
			}
	}


And my validNumbers() is where Im stuck. I started off defining it as
1
2
3
4
5
6
	int validNumbers(const int* array, int count) {

		int valid = count; //I plan on decrementing 'valid' for every invalid phone number
		for (int i = 0; i < count; i++) {
                    //do something
		}


I wanted to do something along the lines of
1
2
3
4
5
6
			if (0 < (numbers[0] && numbers[1]) && 10 > (numbers[0] && numbers[1])) {
				if (sizeof(numbers) == 12) {
					if (0 < (numbers[2] && numbers[3] && numbers[4]) && 10 > (numbers[2] && numbers[3] && numbers[4])) {
						if (0 < (numbers[5] && numbers[6] && numbers[7] && numbers[8] && numbers[9] && numbers[10] && numbers[11]) {
							&& 10 > (numbers[5] && numbers[6] && numbers[7] && numbers[8] && numbers[9] && numbers[10] && numbers[11])) {
							return true;

But I am not sure how to achieve this, as numbers[0] is the first number in phoneNumbers[] (1416123456LL), numbers[1] is 14161234567LL, etc.

Any suggestions?
Last edited on
Why do you try to store long long data type in an int ?
Why don't you store the phone numbers as a string ?

Validating the phonenumber is quite easy with a stringstream and string.
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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

bool isValidPhonenumber(long long phone)
{
  std::stringstream ss;
  ss << phone;
  std::string s = ss.str();
  if (s.length() != 12)
    return false;

  if (s[0] == '0' || s[1] == '0') // can't be true since numbers don't have leading zeros
    return false;
}


int main(int argc, const char * argv[])
{
  long long phoneNumbers[] = 
  { 
    1416123456LL, 14161234567LL, 1416234567890LL,
    14162345678LL, -1LL, 124163456789LL,
    14161230002LL 
  };

  for (auto number : phoneNumbers)
  {
    if (isValidPhonenumber(number))
      cout << number << " is valid\n";
    else
      cout << number << " is not valid\n";
  }
  return 0;
}


To store the phonumbers in the class you better use a std::vector.
Last edited on
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

bool valid_phone_number( long long number )
{
    return number < 1'000'000'000'000 // less than 13 digits
           && number > 99'999'999'999 // 12 digits with first digit non-zero
           && ( number / 10'000'000'000 ) % 10 != 0 ; // and second digit non-zero
}

int main()
{
    const long long numbers[] = { 1416123456LL, 14161234567LL, 1416234567890LL,
                                  14162345678LL, -1LL, 124163456789LL,  14161230002LL,
                                  104163456789LL, -124163456789LL } ;

    for( long long n : numbers )
        std::cout << n << ' ' << std::boolalpha << valid_phone_number(n) << '\n' ;
}

http://coliru.stacked-crooked.com/a/e6d494a5de68d9aa

And do yourself a favour: use std::string for the name
and std::vector<long long> to hold the valid phone numbers.
https://cal-linux.com/tutorials/strings.html
https://cal-linux.com/tutorials/vectors.html

1
2
3
4
5
class Contact {

    std::string name ;
    std::vector< long long > phone_numbers ;
    // ... 


Consider using unsigned long long for phone numbers.
Topic archived. No new replies allowed.