newbie needs help with strtok function

This is a discussion on newbie needs help with strtok function within the C++ Programming forums, part of the General Programming Boards category; Need to write a program that inputs a telephone number as a string in the form (608) 123-1234. This program ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Unhappy 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.
    tinkerbelle

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What've you tried so far?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    13
    So new, that I haven't gotten anything so far...this one has me stumped. Can't even get started descently.
    tinkerbelle

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    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. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    7
    does ptr* mean pointer?

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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. #8
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    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");
    			
    		}
    		//if the number is 11 digits and doesn't start with 1
    		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")
    		{
    			PhoneError("A call cannot start with a 0 or a 1");
    		}
    		//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. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Unhappy 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;
    }
    tinkerbelle

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Thanks, got it

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

    Thanks again.
    tinkerbelle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21