Page 1 of 1

Tokens

Posted: Wed Jul 01, 2009 12:35 am
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

Re: Tokens

Posted: Wed Jul 01, 2009 1:16 am
by XZodia
What does strtok do? and what are its parameters?

Re: Tokens

Posted: Wed Jul 01, 2009 1:37 am
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, "");

Re: Tokens

Posted: Wed Jul 01, 2009 6:41 pm
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.

Re: Tokens

Posted: Wed Jul 01, 2009 7:09 pm
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.

Re: Tokens

Posted: Thu Jul 02, 2009 12:14 am
by Aumaan Anubis
All good :D

Did a little research and got it done.
Thanks.