Hello,
I'm having a bit issue with ispunct in my function. The user can input a phone number in any of the formats below:
4147778888
(414)7778888
(414)777-8888
The program will only print out:
41447778888
What I am trying to do is accept any integer and parentheses. If the user inputs anything else, the program will display an error message.
Right now, parentheses displays an error which is obvious but I'm wondering if there is a way I can allow a parentheses to be entered but not any other symbol. ex: . , : ; " '
Thank you.
Code:
void getPhoneNum()
{
// Local Declarations
char string[14];
char* tokPtr;
// Statements
printf("Enter your seven digit phone number: (ex: (111)111-1111\n");
scanf("%s", string);
while (!isdigit(string[0]))
{
while (ispunct(string[0]))
{
printf ("Please enter numbers and parentheses only!\n");
scanf("%s", string);
}
tokPtr = strtok(string, "()-");
printf("The phone number you entered is: ");
}
while (tokPtr != NULL)
{
printf("%s", tokPtr);
tokPtr = strtok (NULL, "()-");
}
printf("\n\n");
return;
}