1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to input a telephone number and use function strtok to extract?

Discussion in 'C++' started by san gabriel, Jun 4, 2009.

  1. san gabriel

    san gabriel New Member

    Joined:
    Jun 4, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    output should like that:

    input #: (909)869-2511
    area: (909)
    exchange: 869
    ext.: 2511
    #: (909)869-2511

    this is my code so far:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        char sentence[32];
        char *tokenPtr;
        tokenPtr = &sentence;
        
        cout << "Input Telephone No.: " ;
        cin >> sentence;
        tokenPtr = strtok( sentence, " "  );
    
        while ( tokenPtr != NULL ) 
        {
            cout << tokenPtr << '\n';
            tokenPtr = strtok( NULL, " " );
        }
        return 0;
    }
     
    Last edited by a moderator: Jun 4, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    I don't think I'd use strtok for that, just loop through the input string and pick out groups of digits.
     
  3. Nickyrash

    Nickyrash New Member

    Joined:
    Nov 9, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Thanks for sharing this i will try it
     
  4. Nickyrash

    Nickyrash New Member

    Joined:
    Nov 9, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Thanks for sharing this i will try it
     

Share This Page