Thread: Ignore hypens

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    79

    Ignore hypens

    Hi,

    How can I set scanf such that it ignores hypens in the inputs and treats the whole as one number.

    For example, if I enter the phone number 613-8933-5945 then I would like the input to be treated as '61389335945'.

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You can't.

    There are a few options for what you can do. You could read it as a string, and copy the relevant characters to a new string (using isdigit() to figure out which is a digit). You could read it character by character with getc(), storing only the relevant characters (this is a slight variant on the previous suggestion).

    There are other options, but I figure the above would be pretty easy to implement.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Code:
    #include<stdio.h>
    main()
    {
    
            int a,b,c;
            scanf("%d-%d-%d",&a,&b,&c);
            printf("%d%d%d",a,b,c);
    }

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    *smacks forehead*

    Cobmine murugaperumal's method with sprintf() (snprintf() is even better if it's available) and you probably have an ideal approach for the original request.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Sorry can't use strings.

    I would also need to accept the numbers in the non hypened form.... so how would I also accomodate for that?

    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I think that it's not possible with scanf. You can use simple regex with scanf but only on strings. Do you have to use scanf?

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by Subsonics View Post
    Do you have to use scanf?
    Yes

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Sorry can't use strings.
    You mis-understood the requirement, or you're screwed.

    The number you show as an example won't even fit in an integer on most common 32-bit machines.

    If you have a phone number with leading zeros, then these will BE LOST when trying to read it as an integer.

    > 613-8933-5945 then I would like the input to be treated as '61389335945'.
    If you're expecting to be able to cope with both forms (with and without -), then you have to use strings.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ignore paths???
    By gransken in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2008, 05:30 AM
  2. Is (ignore) valid code?
    By Bassquake in forum C Programming
    Replies: 7
    Last Post: 06-10-2008, 08:16 AM
  3. question about ignore()????
    By newbie02 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2003, 08:27 AM