Tokens
Posted: Wed Jul 01, 2009 12:35 am
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.
INCORRECT OUTPUT:
Some guidance as to how to get the third token working would be appreciated.
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");
}
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