简体   繁体   中英

Phone Number Validation in C++?

#include <iostream>
#include "Contact.h"

using namespace std;

using namespace sict;

int main()

{

cout << "----------------------------------------" << endl;

cout << "Testing the default constructor!" << endl;
cout << "----------------------------------------" << endl;
sict::Contact empty; // sict:: intentional
empty.display();
cout << "----------------------------------------" << endl << endl;

cout << "----------------------------------------" << endl;
cout << "Testing an invalid contact!" << endl;
cout << "----------------------------------------" << endl;
Contact bad(nullptr, nullptr, 0);
bad.display();
Contact alsoBad("", nullptr, 0);
alsoBad.display();
cout << "----------------------------------------" << endl << endl;

cout << "----------------------------------------" << endl;
cout << "Testing the constructor with parameters!" << endl;
cout << "----------------------------------------" << endl;
Contact temp("A very long name for contact!", nullptr, 0);
temp.display();
cout << "----------------------------------------" << endl << endl;

cout << "----------------------------------------" << endl;
cout << "Testing a valid contact!" << endl;
cout << "----------------------------------------" << endl;
long long phoneNumbers[] = { 1416123456LL, // invalid: no country code
                               14161234567LL,
                             1416234567890LL, // invalid: wrong country code
                               14162345678LL,
                               10162345678LL, // invalid: wrong area code
                                        -1LL, // invalid: all components are wrong
                              124163456789LL,
                               14160345678LL, // invalid: wrong phone component
                               14161230002LL
};
Contact someContact("John Doe", phoneNumbers, 9);
someContact.display();
cout << "----------------------------------------" << endl << endl;

return 0;
}

I have been given this piece of code and i have to use a function to check if the phone numbers inside "long long phoneNumbers[]" are valid.

Following needs should be met for the phone number to be valid:

A valid phone number has one or two digits for the country code (cannot be zero)

Has exactly three digits for the area code (cannot start with zero)

and has exactly even digits for the number (cannot start with zero);

I am having difficulty in breaking down the code into sections for area code, country code and the main phone number due to which i cannot validate the phone number.

Thanks for anyone helping!

You can use 2 approaches:

  1. convert the phone number to a string, and then simply iterate over it.
  2. Separate the phone number as an integer to multiple numbers. For example, if you want to get the last 2 digits of a number, you'll do this:

     last_2_digits = original_number % 100;

Good luck with that~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM