CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Posts
    7

    phone number validation and looping

    Hello everyone,
    part of my program will have the user input a phone number. My program will validate that number to make sure it matches the format (###-###-####). It must have dashes and all digits in the correct position. I think I have the validation part correct, but my looping is all off. For example, if I input the correct format it goes through an endless loop; on the other hand, if I enter a wrong format it will ask to re-enter a new phone number and run it again. If I enter another wrong number, it accepts it and continues. I have looked at it in different angles but I'm not sure how to get past it.
    (note) the program is setup so the validation and looping must be done within the same function. Any help would be greatly appreciated. Thank you.

    Here is my code:
    Code:
    //***************************************************************************
    void phoneValidation (string& phone)
    {
    	
    	//variables
    	bool invalidFormat = false;
    	char dash = '-';
        
        do
        {
        	for( int count = 0 ; count < phone.length() ; ++count )
        	{
        		if (!isdigit(phone[count]))
        		{
        			invalidFormat = true;
        			if (count==3 && phone[count] != dash || count==7 && phone[count] !=dash)
    		    		invalidFormat = true;
    		    	else
    		    		invalidFormat = false;
    			}
    		}
    		if (phone.length() != 12)
    			invalidFormat = true;
    			
    		if (invalidFormat == true)						// if false than re-prompt question
    		{
    			cout << endl;
    			cout << "ERROR! Invalid format (format: ###-###-####). Re-enter phone number: " << endl;
    			cin  >> phone;
    		}
    	}while (!invalidFormat);
    	return;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: phone number validation and looping

    I suggest that you separate this function into two functions:
    Code:
    bool checkValidPhoneNumber(const std::string& phoneNumber);
    void readPhoneNumber(std::string& phoneNumber);
    You would place all the logic for validating the phone number into checkValidPhoneNumber. For this, I suggest that you check that the length is equal to 12 first. Once you know that the length is valid, you can go on to check for digits and dashes in the right positions without worrying that you might be accessing the string out of bounds.

    Now, checkValidPhoneNumber will only validate, i.e., it does one thing and does it well. It will not request for input or read input. Instead, you place the logic for getting input and obtaining new input if the phone number is invalid into readPhoneNumber. Notice that I declared the phoneNumber parameter of checkValidPhoneNumber to be a const lvalue reference: it will not modify the phone number, whereas phoneNumber in readPhoneNumber is a non-const lvalue reference as you will presumably store the phone number read into that.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2015
    Posts
    208

    Re: phone number validation and looping

    Quote Originally Posted by jpanther View Post
    matches the format (###-###-####)
    Another approach is to use regular expressions for pattern matching. In this case it's pretty simple and straightforward,

    Code:
    if (std::regex_match(phone, std::regex("[0-9]{3}-[0-9]{3}-[0-9]{4}") )) {
        // phone number is ok
    }
    Last edited by tiliavirga; September 2nd, 2015 at 02:56 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured