# Thread: newbie needs help with strtok function

1. ## newbie needs help with strtok function

Need to write a program that inputs a telephone number as a string in the form (608) 123-1234. This program should use strtok function to extract the area code as a token, the first three digits of the number as a token, and the last four digits of the number as a token as well. The seven digits of the phone number should be concatenated into one string. The program should convert the area code string to int and convert the phone number string to long. Both the area code and the phone number should be printed out.

2. What've you tried so far?

3. So new, that I haven't gotten anything so far...this one has me stumped. Can't even get started descently.

4. Code:
`scanf("%[^(]%*c%[^0-9]%d%[^)]%*c%[^0-9]%d%[^-]%*c%[^0-9]%d", &areacode, &prefix, &suffix );`

Personally I dislike strtok. However, it is a good exercise in pointer management.

Quzah.

5. Why bother with strtok, why not use the string class.
To get you started :

Code:
```#include <iostream>
#include <stdlib.h>
#include <string>

int main()
{
string phone;
cout << "input phone number : \t";
getline(cin, phone);

system("PAUSE");
return 0;
}```
You'll find all the methods you need right here

6. does ptr* mean pointer?

7. ptr* means whatever ptr is multiplied by whatever comes after ptr, ptr is just a name for, I would assume, a variable of pointer type, but people can be stupid about such things.

int* ptr; // ptr is a variable of type pointer to int
int *ptr; // same thing
ptr = 0; // sets the pointer to point to nothing
*ptr = 0; // sets whatever int lives at the address held in pointer to zero, if ptr == 0 this will probably crash, unless you are relying on the program crashing, in this case the program will send gay porn to your mother.

8. This dude is probably from Penn State taking
either CMPSC 201c or CSE 103 (or variants of the same
course)

But anyway... here's what I did when I took that
class
Code:
```//[MOONWALKER]
//Lab assignment 13

#include <iostream>
#include <string>

using namespace std;

//precondition: a sting message is passed
//postcondition: displays an error and terminates the program
void PhoneError(string message)
{
//showing the error message intended
cout << "Your call cannot be completed because: " << endl;
cout << message << endl;
//exiting the program
exit(1);

}

//precondition: a sting is passed
//postcondition: nothing happens if it is valid. otherwise program terminates
void CheckValidity(string aString)
{

int i;

//if the size is other than 7 or 11
if ( aString.size() != 11 &&  aString.size() != 7)
{
PhoneError("A phone number should have 7 or 11 characters");

}
else if (aString.size() == 11 && aString.substr(0,1) != "1")
{
PhoneError("A long distance call must begin with 1");
}
//if the number is 7 digits and starts with a 0 or a 1
else if (aString.size() == 7 && aString.substr(0,1) == "1" || aString.substr(0,1) == "0")
{
}
//checking if the number contains any illegal characters
for (i = 0; i < aString.size(); i++)
{
if (aString.substr(i, 1) < "0" || aString.substr(i, 1) > "9")
{
PhoneError("A phone number must consist of all digits");
}
}
}

int main()
{
//setting up variables
string phonenumber, newformat;

//taking the input
cout << "Enter a phone number: ";
cin >> phonenumber;

//checking the validity of the input using the CheckValidity function
CheckValidity(phonenumber);

//showing the output based on the size of the given number
if (phonenumber.size() == 11)
{
newformat = "(" + phonenumber.substr(1, 3) + ")-" + phonenumber.substr(4, 3) + "-" + phonenumber.substr(7, 4);
}
else if(phonenumber.size() == 7)
{
newformat = phonenumber.substr(0,3)+ "-" + phonenumber.substr(3, 4);
}

//printing the number as desired
cout << newformat << endl;

return 0;

}```

9. ## strtok and using int & long for phone number

This is what I have so far on this phone number program using the instructions above but it still isn't working correctly. HELP!
#include <string.h>
#include <iostream>
using std::cout;
using std::cin;
int main(int argc, char **argv)
{
char phone[]="(areacode) prefix-suffix";
char *areacode , *prefix , *suffix;
char *toks1 = " -";
char *toks = "()";
char *end = phone+strlen(phone);

areacode = strtok( phone, toks );
prefix = strtok( NULL, toks1 );
suffix = strtok( NULL, toks1 );

cout <<"Enter your phone number using this format (nnn) nnn- nnnn:";
cin >> areacode >> prefix >> suffix;
printf( "areacode=(%s) , prefix=%s, suffix=%s\n", areacode, prefix, suffix );
cout << "\nstrcat (areacode, prefix, suffix) = " << strcpy(end, phone) << strcat(prefix, suffix);

return 0;
}

10. ## Thanks, got it

Thanks for info, I managed to figure it out so it would work the way it needed to.

Thanks again.