hello everyone, I am trying to validate a phone number so the program will check if the user inputted this format (123-45-6789). The phone number is inputted as a string character and I am trying to use a for loop to check each individual character. Within this same function, I am also trying to have it loop until the user inputs the correct phone number. Here is what I have so far:
This functions purpose is when the user selects 'A'(add) from the menu and then takes you to other functions.
//*************************************************************
void pickingOptions(char selection, ifstream& inRentalData, apartmentInfo addApartment[], int& cnt)
{
// variable
int num;
string phoneNumber;
float rentAmount;
float status;
char yesNo;
bool inserted = false;
switch (selection)
{
case'S':
// Display all the apartment information on file
displayRental(addApartment, cnt);
break;
case'A':
//loop to keep adding apartments until user is done
cout << "---- The following 3 questions are for adding an apartment ----" << endl << endl;
while(!inserted)
{
// phone number prompt and validation
phonePrompt (PHONE_PROMPT, phoneNumber, cnt, inserted);
phoneValidation (phoneNumber);
addApartment[cnt].phoneNum = phoneNumber;
// rent amount prompt and validation
rentAndStatusPrompt (RENT_PROMPT, rentAmount, cnt, inserted);
rentValidation (rentAmount);
addApartment[cnt].rent = rentAmount;
// rental status prompt and validation
rentAndStatusPrompt (STATUS_PROMPT, status, cnt, inserted);
statusValidation (status);
addApartment[cnt].rentalStatus = status;
// prompt user to continue or not
cout << "---- Do you wish to add more apartments to the list (Y/N)? ----" <<endl;
cin >> yesNo;
yesNo = toupper(yesNo);
if (yesNo == 'Y')
inserted = false;
else
inserted = true;
// add counter to array
cnt++;
}
break;
case'D':
//deleteApartment();
break;
}
return;
}
//**********************************************************
This function prompts the user for a phone number and returns the value via references parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void phonePrompt (string prompt, string& promptAnwser, int& listSize, bool& test)
{
if (listSize == MAX_ARRAY) // check to see if Array is full
{
cout << "ERROR - No more apartments can be added to list!" << endl;
test = true;
}
else // proceed if array is NOT full
{
cout << prompt << endl;
cin >> promptAnwser;
} // end else
return;
}
//**********************************************************
This function is supposed to validate the phone number and continue to loop until the user gets the correct format. This is where I am stuck.
//***************************************************************************
void phoneValidation (string& phone)
{
//variables
bool invalidFormat = false;
//will loop as long as format is incorrect (bool = true)
do
{
char dash = '-';
for (int i= 0; i < phone.length()-1; i++)
{
if (i == 3 || i == 6)
phone[i] == dash ;
else
{
isalpha(phone[i]);
invalidFormat = true;
}
}
// if total characters within the phone number does not equall 12
if (phone.length()!= 12)
invalidFormat = true;
// if bool = true, than re-prompt for new phone number
if (!invalidFormat)
{
cout << "ERROR! Invalid phone number (Format: ###-###-####). Please re-enter your phone number: " << endl;
cin >> phone;
}
}while (!invalidFormat);
return;
}
I am fairly new to this. Any help would be greatly appreciated. Thanks to everyone for your help.
Thank you for your response. Where does this test if I have a dash('-')? Does the regex test for a dash? We haven't covered regex yet, so I don't think I'm supposed to use that. Thanks again JLBorges.
So I changed a few things around, so the function will re-prompt for another number if it is wrong. This actually works. Thank you for pointing me in the right direction.