Tokens

Discuss anything programming related.
Post Reply
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Tokens

Post by Aumaan Anubis »

Problem: Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the phone number as a token, and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed.

I've begun, but I've run into trouble.

Code: Select all

// Lab 8.35.cpp : main project file.

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

using namespace System;

int main()
{	
	char areaCode[6];
	char number[9];
	// char lastNumbers[5];
		
	cout << "Please enter a phone number in the format (xxx) xxx-xxxx" << endl;
	cin >> areaCode >> number;
	
	char *ptrToken;
	char *ptrToken2;
	char *ptrToken3; 
	
	ptrToken = strtok( areaCode, " " );
	ptrToken2 = strtok( number, " ,.-");
	ptrToken3 = strtok (number, " ");

	while(ptrToken != NULL && ptrToken2 != " ")
	{
		cout << ptrToken << "\n";
		ptrToken = strtok(NULL, " ");
		cout << ptrToken2 << "\n";
		ptrToken2 = strtok(NULL, " ");
		cout << ptrToken3 << "\n";
		ptrToken3 = strtok(NULL, " ");
	}
	 cout << "\nAfter strtok, the number = " << areaCode << number << endl;
	 system("pause");
}
INCORRECT OUTPUT:

Code: Select all

Please enter a phone number in the format (xxx) xxx-xxxx. 
(999) 999-9999            < Input
(999)
999
999
After strtok, the number = (999)999
Some guidance as to how to get the third token working would be appreciated. :D
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

Re: Tokens

Post by XZodia »

What does strtok do? and what are its parameters?
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
Grimdoomer
Admin
Posts: 1835
Joined: Sun Dec 09, 2007 9:09 pm

Re: Tokens

Post by Grimdoomer »

Code: Select all

ptrToken = strtok( areaCode, " " );
ptrToken2 = strtok( number, " ,.-");
ptrToken3 = strtok (number, " ");
I think this is is your problem.

Code: Select all

ptrToken = strtok( areaCode, " " );
ptrToken2 = strtok( number, "-");
ptrToken3 = strtok (number, "");
Don't snort the magic, we need it for the network.
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Re: Tokens

Post by Aumaan Anubis »

That was not it, Grim :\.

Xzodia, strtok breaks a string into a series of tokens. The first parameter is the string to be tokenized, the second parameter is the delimiting character(the character the represents the end of a token.)

strtok starts at the beginning of the string, and moves through it until it finds the delimiting factor.
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

Re: Tokens

Post by XZodia »

Well first of all, I'm guessing for the point of this exercise, the following should be done differently because currently its already splitting the string input into 2 parts:

Code: Select all

   char areaCode[6];
   char number[9];
   // char lastNumbers[5];
      
   cout << "Please enter a phone number in the format (xxx) xxx-xxxx" << endl;
   cin >> areaCode >> number;
Once you've fixed that come back if you still need help.
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
Aumaan Anubis
Staff
Posts: 1812
Joined: Thu Dec 13, 2007 12:18 am
Contact:

Re: Tokens

Post by Aumaan Anubis »

All good :D

Did a little research and got it done.
Thanks.
Post Reply