-
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;
}
:confused:
-
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;
}
-
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.
-
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);