Thread: telephone string

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Question telephone string

    my code is supposed to take a phone# as a string in (555) 555-5555 type of form using a strotk to take out the area code as a token, the 1st 3 digigts as another token, the last 4 as another token the 7 digits should be concatenated into 1 string it sould alos convert the area code string to an int and the phone # to a long and print the number
    and it should be looped 3 times here is the code what is wrong with this stuoid thing:

    #include "stdafx.h"
    #include <cstring>
    #include <iostream>
    using namespace std;

    int main()
    { char *phone[15];
    char *area[7];
    char *num[10];
    int areaCode;
    char* tokenPtr;
    long int pnum;
    for (int i=0; i<3; i++){
    cout<<"Enter a phone number[(###) ###-####]: ";
    cin.getline(phone,15);
    strtok(num, "()");
    areaCode=atoi(area);
    strtok(NULL, "-");
    strcpy(num,phone);
    strtok(NULL, "");
    strcat(phone,num0);
    pnum=atol(phone);
    cout<<area<<num;}
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I take it you use MSVC++?
    This should work, but I'm not able to test it at the moment so you're on your own for debugging.
    Code:
    #include "stdafx.h"
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int main(){ 
    	char phone[15]; char area[7]; char num[10]; char* tokenPtr;
    	int areaCode;
    	long int pnum;
    
     	cout << "Enter a phone number [(###) ###-####]: ";
    	cin.getline(phone, 15);
    	tokenPtr = strtok(phone, "()- ");
    	area = tokenPtr; areaCode = atoi(area);
    
    	for(int i = 0; i < 3; i++){
    		tokenPtr = strtok(NULL, "()- ");
    		strcat(num, tokenPtr);
    	}
    	cout << areaCode << atol(num) << endl;
    	return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    it did help but i still have aproblem w/
    area = tokenPtr;

    it says
    error C2440: '=' : cannot convert from 'char *' to 'char [7]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    Error executing cl.exe.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    strtok returns a char * which is different from a char [] although both can be used to represent a string. The compiler says it can't find a prototype to convert the rhs to the left. I would try using strcpy() instead of assignment operator =;

    strcpy(area, tokenPtr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM