Thread: Validating 'phone number

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    Validating 'phone number

    Im trying to loop through an array (held inside a structure) to see if it only contains 0,1,2,3,4,5,6,7,8,9 (or a space). This is to check it is a telephone number.

    Code:
    do
    	{
    	cout<<"Telephone Number: ";
        cin.getline(mems[total_members+1].tel_num,sizeof(mems[total_members+1].tel_num),'\n');  
        tel_wrong = 0;	
    	for(counter=0;counter<13;counter++)
    		{
    	    
    		if(mems[total_members+1].tel_num[counter] != ' ' ||   mems[total_members+1].tel_num[counter] != '0' ||
                      mems[total_members+1].tel_num[counter] != '1' || 
                      mems[total_members+1].tel_num[counter] != '2' || 
                      mems[total_members+1].tel_num[counter] != '3' || 
                      mems[total_members+1].tel_num[counter] != '4' || 
                     mems[total_members+1].tel_num[counter] != '5' || 
                     mems[total_members+1].tel_num[counter] != '6' || 
                     mems[total_members+1].tel_num[counter] != '7' || 
                     mems[total_members+1].tel_num[counter] != '8' || 
                     mems[total_members+1].tel_num[counter] != '9') tel_wrong = 1;
    		}
    	}
    	while(tel_wrong == 1);
    even when I put in a valid phone number it still asks me to enter it again!

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    #include <string.h>
    
    using namespace std;
    
    
    int main()
    {
        char text[81];
        cout<<"Enter string:";
        cin.getline(text,81);
        
        char numbers[12]= {" 0123456789"};
       
        int size_of=strlen(text);
        
        
       
        
        
        
        //loop through each array to check for validity
        
       
        int counter=0;
        
        for (int a=0; a<size_of; a++)
        {
           for (int b=0; b<11; b++)
           {
               if (text[a]==numbers[b])
               {   
                   counter++;
               }
           }
        } 
                      
       if (counter==size_of)
       {   
           cout<<"Contains only numbers therefore valid";
           
       }    
       
        else 
        cout<<"Contains letters therefore invalid";
         
            
        
        
        
        
        
        
        int stop;
        cin>>stop;
    
    
    }
    I hope this helps!

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    It'd be even easier to use the isdigit() and isspace() functions in <cctype>.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Thanks,

    ive tried this:
    Code:
    int test=0;
    	int tel_wrong=0;
    	do
    	{
    	cout<<"Telephone Number: ";
        cin.getline(mems[total_members+1].tel_num,sizeof(mems[total_members+1].tel_num),'\n');  
    
    	for(counter=0;counter<13;counter++)
    		{
    
    		test=isdigit(mems[total_members+1].tel_num[counter]);
    		test=isspace(mems[total_members+1].tel_num[counter]);
    		if(test==0)tel_wrong=1;
    		}
    
    		
    	}
    	while(tel_wrong == 1);
    But it still wont accept even a valid phone number!

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Your logic inside the for loop is faulty. You're reassigning test without checking its value. Because of this, tel_wrong will be true only when the character you're looking at is a space.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    ive tried adding extra variables:

    Code:
    int test=0;
    	int test2=0;
    	int tel_wrong=0;
    	int tel_wrong2=0;
    	do
    	{
    	cout<<"Telephone Number: ";
        cin.getline(mems[total_members+1].tel_num,sizeof(mems[total_members+1].tel_num),'\n');  
    	for(counter=0;counter<13;counter++)
    		{
    
    		test=isdigit(mems[total_members+1].tel_num[counter]);
    		test2=isspace(mems[total_members+1].tel_num[counter]);
    		if(test==0)tel_wrong=1;
    		if(test2==0)tel_wrong2=1;
    		}
    
    		
    	}
    	while(tel_wrong == 1 || tel_wrong2 == 1);
    but still doesnt work.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Learn to debug your code. Rather than trying to learn to use a debugger, for something like this just put in a bunch of couts to see where the problem is. That'll help narrow some things down for you.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    You're checking if the current place is a space, and if it isn't you're setting tel_wrong2 to 1.
    Then you're checking the current is place is a digit, and if it is't you're setting tel_wrong to 1.

    Since there are no characters which is a space and a number at the same time, you will always get a "wrong" number.

    try this instead:
    Code:
    if(!isdigit(mems[total_members+1].tel_num[counter]) && 
       !isspace(mems[total_members+1].tel_num[counter]))
       tel_wrong = 1;

  9. #9
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Try using find_first_not_of function for strings. If you find a match, it's invalid:

    Code:
    <string>
    <iostream>
    using namespace std;
    
    int main()
    {
        string phone = "999-934-2341";
        string valid ="0123456789-";
    
        size_t pos = phone.find_first_not_of(valid);
        if (pos != string::npos) {
            cout << "It's invalid" << endl;
        }
        return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  10. #10
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Quote Originally Posted by FlyingDutchMan
    You're checking if the current place is a space, and if it isn't you're setting tel_wrong2 to 1.
    Then you're checking the current is place is a digit, and if it is't you're setting tel_wrong to 1.

    Since there are no characters which is a space and a number at the same time, you will always get a "wrong" number.

    try this instead:
    Code:
    if(!isdigit(mems[total_members+1].tel_num[counter]) && 
       !isspace(mems[total_members+1].tel_num[counter]))
       tel_wrong = 1;
    Thanks that helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Phone number to word conversion program
    By lostmyshadow in forum C Programming
    Replies: 12
    Last Post: 04-21-2009, 02:31 PM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Classes using phone numbers
    By correlcj in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2002, 10:17 PM
  4. Phone number
    By beachchutney in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 04:18 PM