0

I am trying to validate the phone numbers entered by the user. the number has to be entered as xxxxxxxxxx not xxx-xxx-xxxx or anything else. If what is entered contains anything that isn't a number it should be considered invalid and the user has to reenter the number. So far my programs works if the invalid input is entered at the beginning, however, I want to check if any of what is entered is not a number. For example, klsj456778, --87389474, and 23708-- should all be considered invalid inputs. With the program I have right now, only the first two are considered invalid and require the user to reenter the number. Also, I have to keep the phone variable an int datatype which is why I don't just change it to a string. Is it possible to do this? I've been looking at different resources and haven't been able to find an answer.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    long long phone;
    string check;
    bool status;
    string fail = "";
    //validate input
    do
    {
        cout << "Enter phone number(without - or letters): ";
        cin>>phone;

        //in order to type check
        if (cin.fail())
        {
            //you have to clear these before continuing
            cin.clear();
            cin.ignore(256, '\n');
            cout << "Invalid Input" << endl;
        }
        else
        {

            check = to_string(phone);

            if (check.length() == 10)
            {
                if (check.substr(0, 3) == "405" || check.substr(0, 3) == "520" || check.substr(0, 3) == "550")
                {
                    status = true;
                }
                else
                {
                    status = false;
                }
                break;
            }
            else
            {
                status = false;
            }

            break;
        }

    } while (true);
    if (status == true)
    {
        cout << "\nPhone number: " << phone << endl;
        cout << "Verification Status: Valid";
    }
    else
    {
        cout << "Phone number: " << phone << endl;
        cout << "Verification Status: Invalid";
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • You could read it in as a string because space and hypens are usually valid formattings for phone numbers. Checkout `std::regex`, specifically [`std::regex_match`](https://en.cppreference.com/w/cpp/regex/regex_match). – Mansoor Sep 08 '20 at 23:40
  • i would but i have to keep the phone variable as a integer datatype – Abiyah Cherian Sep 08 '20 at 23:44
  • If the phone number has to be an integer data type, then it can't possibly contain anything other than numeric digits. A letter would automatically prevent the value from being converted to an int. And if the number has to be an integer, which prevents you from using a regex, then why does your code use `check = to_string(phone)`? – Ken White Sep 08 '20 at 23:45
  • We hear you about needing to use an integer, but note that phone numbers are not well suited to being integers. For example there's the problem of storing leading zeroes. A phone number is more of a code than a number and should be stored as a string. – user4581301 Sep 08 '20 at 23:51

0 Answers0