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";
}
}