checking for dashes in a phone number

Hello everyone, I just got done posting a similar question before and I thought it was all fixed. When I went to do more detailed tests, it turned out that my program wasn't testing for dashes. As long as the format was ###-###-####, the '-' could be any symbol and it would still work. I need it to be a dash. Here is what I have so far. Thank you for your help.


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
38
39
40
  //***************************************************************************
void phoneValidation (string& phone)
{
	//variables
	bool invalidFormat = false;
	
	while (!invalidFormat)
	{
		// initial loop to check the users phone number. One character at a time
	    for( int i = 0 ; i < phone.size() ; ++i )
	    {
	        if( PATTERN[i] == 'd' ) 			// digit expected
	        {
	            if( !isdigit(phone[i])) 		//If there is not a digit return false
					invalidFormat = false ;
				else							
					invalidFormat = true ;
	        }
	        else if( PATTERN[i] != phone[i] ) 	//if the const PATTERN (ddd-ddd-dddd) doesn't = the phone number return false
				invalidFormat = false ;
			else 
				invalidFormat = true ;
		
	    }
	    if( phone.size() != PATTERN.size() ) 	// if the number of characters in the phone number doesn't = the pattern size return false
			invalidFormat = false ;
		else
			invalidFormat = true ;
		
        if (!invalidFormat)						// if false than re-prompt question
		{
			cout << endl;
			cout << "ERROR! Invalid phone number(format: ###-###-####). Re-enter phone number: " << endl;
			cin  >> phone;
		}
		else	
			invalidFormat = true;	
	}
    return;
}

This is where the function is called from:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//*************************************************************
void pickingOptions(char selection, ifstream& inRentalData, apartmentInfo addApartment[], int& cnt)
{
	// variable
	int num;
    string phoneNumber;
    string numDelete;
    int locatedPhoneNum;
    float rentAmount;
    float status;
    char yesNo;
    bool inserted = false;
    bool deletedPhoneNum = false;
    bool goodPhone = 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
				goodPhone = false;
				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':
			//display the availble phone numbers 
			displayPhoneNumber(addApartment, cnt);
			//user prompt/read for phone number
			numDelete = deletePhonePrompt();
			//validate
			phoneValidation(numDelete);
			// find and delete phone number in the array
			locatedPhoneNum = findAndDeleteNum(numDelete, addApartment, cnt, deletedPhoneNum);
			//display message that file was deleted or messafge that file not found
			displayDeleteMessage(numDelete, locatedPhoneNum, addApartment, cnt);
			break;				
	}
	return;
}

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
bool valid_phone( std::string str )
{
    static const std::string pattern = "(ddd-dd-dddd)" ; // use <regex> ?

    if( str.size() != pattern.size() ) return false ;

    for( std::size_t i = 0 ; i < str.size() ; ++i )
    {
        if( pattern[i] == 'd' ) // digit expected
        {
            if( !std::isdigit( str[i] ) ) return false ;
        }
        else if( pattern[i] != str[i] ) return false ;
    }

    return true ;
}

void phone_validation( std::string& phone )
{
    while( !valid_phone(phone) )
    {
        std::cout << "\nERROR! Invalid phone number '" << phone
                  << "' format: (###-###-####)\nRe-enter phone number: " ;
        std::cin >> phone ;
    }
}
JLBorges,
Thank you for your input. This really helped me to see how to validate using a pre-defined pattern. New ways of looking at things really helps. Thank you.
Topic archived. No new replies allowed.