output should like that:

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

this is my code so far:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstring>
using std::strtok;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	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;
}

you really don't need to use strtok() for this problem. All you need is strchr(). First locate the ')' charcter, copy that to areaCode character array. Then find the '-' character, copy that to exthange character array. Finally copy the remaining characters to the ext array.

If you know how to use pointers, then you don't need strchr() either.

here a better version:

Problems: the close parentheses is missing :-p

#include <iostream>
#include <iomanip>
#include <cstring>
using std::strtok;
using namespace std;

int main()
{
    char sentence[32];
    char *tokenPtr;
    
    cout << "Input Telephone No.: " ;
    cin >> sentence;
    
    tokenPtr = strtok( sentence, ")-" );
    
    while ( tokenPtr != NULL )
    {
        cout << tokenPtr << '\n';
        tokenPtr = strtok( NULL, ")-" );
    }
    
    return 0;
}

Read Ancient Dragons post again.

Problems: the close parentheses is missing :-p

You won't get the required output because strtok uses the character within " " in strtok(str," ") as the delimiter and uses it to cut the string rather than including it.This explains why the closing paranthesis is missing.

One silly but quick correction you can do is this :

tokenPtr = strtok( sentence, ")" );
cout << tokenPtr << ')' << '\n';
tokenPtr = strtok( NULL, "-" );
while ( tokenPtr != NULL )
{
       	cout << tokenPtr << '\n';
        tokenPtr = strtok( NULL, "-" );
}

Well, why are you not using std::strings?
I wrote a quick code with those. It is quite flexible:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string input="(98415)4554-215245";
    string area,exchange,extention;
    
    {//these are added to limit the scope of c_brace & hyphen
        size_t c_brace=input.find(")");//closing brace
        size_t hyphen=input.find("-");//the hyphen
        area=input.substr(  1, c_brace -1);
        exchange= input.substr( c_brace+1, hyphen-c_brace-1);
        extention= input.substr(hyphen+1,input.size()-hyphen);
    }
    
    cout<<area<<" "<<exchange<<" "<<extention;
    return 0;
}

(Oh my. Python has started to influence my Cplusplus)
I know few advocates can sue me to give away code, but I am here demonstrating the power of using STL (the computational code is just 5 lines)

commented: Stop your spoon feeding :) -4

>Stop your spoon feeding
What are you upto my life. Just because I gave you infraction ones, you've started a mission to give me infractions.
I have made it already clear Why I delivered the code. Besides, my code was not as rusted as you usually post.
:(

Hi, siddhant3s, I think it would be more helpful to the OP if you indent your code, there's a nice article about code indenting available here :)

>>Hi, siddhant3s, I think it would be more helpful to the OP if you indent your code
Dear Tux4life, the code has been already indented by Astyle Free source code intender which is integrated on my Code::Blocks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.