Validating a phone number in C++


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Validating a phone number in C++

  1. #1
    Join Date
    May 2009
    Posts
    6

    Question Validating a phone number in C++

    hey guys, I need to take a phone number in in the format of "(555)555-5555"

    This is how I did it:

    Code:
    void CustomerData::setPhone(string temp)throw (string){
        bool correct[10] = {false};
        char possible[10] = {'1','2','3','4','5','6','7','8','9','0'};
        bool good = true;
    
        for(int i = 0; i<10; i++){
            if(temp.at(1)==possible[i]) correct[0] = true;
            if(temp.at(2)==possible[i]) correct[1] = true;
            if(temp.at(3)==possible[i]) correct[2] = true;
            if(temp.at(5)==possible[i]) correct[3] = true;
            if(temp.at(6)==possible[i]) correct[4] = true;
            if(temp.at(7)==possible[i]) correct[5] = true;
            if(temp.at(9)==possible[i]) correct[6] = true;
            if(temp.at(10)==possible[i]) correct[7] = true;
            if(temp.at(11)==possible[i]) correct[8] = true;
            if(temp.at(12)==possible[i]) correct[9] = false;
        }
        
        for(int i = 0; i < 10; i++){
            cout<<correct[i]<<endl;
            if(correct[i]!= true) good = false;
        }
        
        if(temp.length() != 13 || temp.at(0) != '(' || temp.at(4) != ')' || temp.at(8) != '-' || good == false)
            throw string("Incorrectly Formatted Phone Number\n");
        else
            phone = temp;
    }
    When it doesn't match It goes to an error that says the following in Dev-C++

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.

    Here's my main(where I catch the thrown strings):
    Code:
    #include <fstream>
    #include "CustomerData.h"
    #include "CDRecord.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        //create the file streams and load the files into them
        fstream file;
        fstream ofile;
        file.open("testFile.txt", ios::in);
        ofile.open("newfile.txt", ios::out);
    
        //Create the objects to be used for the input and output
        CustomerData cd;
        CDRecord cdr;
    
        //read in the file and add the information to the 
        //CDRecord object by means of adding the file information
        //to a CustomerData object and adding it to the CDRecord
        //object
        while(!file.eof()){        
            try{
                file>>cd;
                cdr+cd;
            }catch(string m){
                cout<<m;
                break;
            }
        }
    
        //Write all the information to the output file
        ofile<<cdr;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Any help greatly appreciated!

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    You need to break the string down into three components: characters [0] .. [4] then [5]...[8] and [9]...[13]. First, check whether the entered string consists of 13 characters. For the first part: check first whether c[0]=='(' and c[4]==')' then go through c[1] through c[3] and verify that they contain digits (you can use the isdigit() function to simplify this procedure). Define similar validation tests for the remaining parts and remember to pass string to and from functions by reference, not by value.
    Danny Kalev

Similar Threads

  1. Phone Number Formats
    By clarko2122 in forum Database
    Replies: 5
    Last Post: 04-18-2008, 11:40 AM
  2. Replies: 18
    Last Post: 12-13-2006, 03:49 PM
  3. Phone number search
    By Judy in forum VB Classic
    Replies: 4
    Last Post: 08-31-2000, 12:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center