Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   How to input a telephone number and use function strtok to extract? (http://www.go4expert.com/forums/input-telephone-function-strtok-extract-t17854/)

san gabriel 4Jun2009 05:52

How to input a telephone number and use function strtok to extract?
 
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;
}


xpi0t0s 4Jun2009 22:14

Re: How to input a telephone number and use function strtok to extract?
 
I don't think I'd use strtok for that, just loop through the input string and pick out groups of digits.

Nickyrash 9Nov2009 18:30

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

Originally Posted by san gabriel (Post 48969)
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;
}


Thanks for sharing this i will try it

Nickyrash 9Nov2009 18:33

Re: How to input a telephone number and use function strtok to extract?
 
Thanks for sharing this i will try it


All times are GMT +5.5. The time now is 15:25.