Like Tree1Likes
  • 1 Post By Malcolm McLean

ispunct

This is a discussion on ispunct within the C Programming forums, part of the General Programming Boards category; Hello, I'm having a bit issue with ispunct in my function. The user can input a phone number in any ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Concord
    Posts
    4

    ispunct

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

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    283
    Your formatting makes it very difficult to read. Please follow something similar to this Indent style - Wikipedia, the free encyclopedia and repost.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    while on while in while ..

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Just to add to jwroblewski44's comment, you might find that you need to "paste as text" when copy/pasting from your IDE.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,068
    In addition to digits, you want to check for three other symbols - why not just check for them discretely, using logical operators?

    Code:
    if(string[i] != '(' ...

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    331
    Quote Originally Posted by aquari View Post
    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
    You've got two main choices. One is to use regular expressions, which are designed for this job. C has no built in regular expression library, but there's almost always one available.

    The other is to hardcode it. We can easily define a valid character (a digit, a space, a hyphen or a parenthesis). We can easily check parentheses for balance.
    Then to get the raw number, simply strip out any non-digits.
    King Mir likes this.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  7. #7
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    26
    You could hardcode it as mentioned previously:
    Code:
    bool validNumber(char cell[])
    {
        for (int i = 0; cell[i] != '\0'; i++)
        {
            if (!isdigit((int)cell[i]) && cell[i] != '(' && cell[i] != ')' && cell[i] != '-') return 0;
        }
        return 1;
    }
    
    int main()
    {
        char cell1[] = "(414)777-8888";
        char cell2[] = "(414)..777-8888";
        printf("%d\n", validNumber(cell1));
        printf("%d\n", validNumber(cell2));
        return ERROR_SUCCESS;
    }
    Last edited by cstryx; 06-23-2013 at 05:30 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isalpha || ispunct
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 05-21-2004, 09:43 PM

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