Formatting with hyphens

This is a discussion on Formatting with hyphens within the C Programming forums, part of the General Programming Boards category; Hi there, I'm stuck on as to how I can format numbers as below. I want to input something like ...

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

    Formatting with hyphens

    Hi there,

    I'm stuck on as to how I can format numbers as below.

    I want to input something like 930060504857 and make it appear as 930-060504-857-7.

    I already have the scanf and other variables relating to the input set up.

    Also, how can I make it so that the program will only accept integer values?

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Mini View Post
    Hi there,

    I'm stuck on as to how I can format numbers as below.

    I want to input something like 930060504857 and make it appear as 930-060504-857-7.

    I already have the scanf and other variables relating to the input set up.
    That would entirely depend on how you have your "scanf and other variables" defined.
    Quote Originally Posted by Mini View Post
    Also, how can I make it so that the program will only accept integer values?

    Thanks
    I thought you had your scanf set up? If you want integer values, use a variable of integral type and an integer input conversion (like %i). If you mean "if I type in something random, like letters, I want good things to happen", then you need to look at input validation (in the FAQ, say).

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you want to input a large number and don't care about any numeric aspect related to it but rather use it as some identifier, then parse it as a string not an int/long.

    Also you can't make the program only take integers. You need to write code than handles different behavior for the program in terms of what you receive as input. A general rule in programming is that you can assume that the person typing input is a monkey and it presses keys randomly. Your program should still pass the monkey test.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Printf with hyphens?

    Does anyone know the command for printing hyphenated text in printf?
    I have a long int variable read in from scanf as &width, but how can I print it like 457-89-05?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    printf("%d-%d-%0d", height, width, depth);

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Hmm, there is only one variable that needs to be hyphenated, not 3? So

    printf("Width is %ld", width);

    needs to display as 457-89-05, not 4578905.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Hi, Unfortunately I'm not allowed to use strings :S

    I would also need to perform calculations of each digit and compare with the checksum digit. So something that would facilitate for that would be good.

    Here is what I have for scanf for now (which works):

    unsigned long long bc_input;
    scanf("%lld", &bc_input);

    I understand that I need to do this myself etc, but I have no clue as to where I'm supposed to start. Could someone please give me links to tutorials etc that would be relevant for this, or maybe a hint or two?

    Thanks again!

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok.

    In my understanding of what it is you are supposed to do you have two options I can think of:

    1) Represent each integer as the largest integer type available like you are doing now.

    In order to get each of the digits separately write a loop that extracts number%10 (the last digit) and then divides the number by 10 up to the point where you are left with no digits left.

    2) Read the number as individual characters and convert each digit into an int that you store in a int array.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by claudiu View Post
    2) Read the number as individual characters and convert each digit into an int that you store in a int array.
    This looks more appealing. Do you have any tutorials or links as to how I could get started? I know how I could store them into an int, but I don't know how I could read individual characters...

    My teacher hasn't taught me a thing

    Thanks again.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can print it as a string if you:

    1) add the hyphens into it, in the char array

    2) ensure it has an end of string char '\0', at the end of it

    3) use %s in printf("%s", myString); to print it out.

    There is no C function in the library to print up part of a number, then a hyphen, then another part of the number, then another hyphen, and then the last part of the number.

    You can do it with math, of course, (which involves picking the number apart), but that's more difficult than the above way, using a string.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You read character by character using:

    Code:
    char x;
    
    scanf("%c",&x);
    
    /* now you convert the character c into an int using the function atoi(). look it up */
    
    int x_as_int = atoi(x);

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Question Very basic C question

    How do I make a program which hypehenates a user-inputed (big) number?

    For example, if someone typed in their phone number, 9051234567, the program will be able hyphenate it to 905-123-4567.

    The restrictions of this task is that I can't use functions, arrays, or strings. :S I'm super stumped on this, only recently started learning C.

    Thanks in advance!

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Uh oh, I don't think I'm allowed to use that command either :'( ... Looks like I'll have to use Option 1 then...


    Quote Originally Posted by claudiu View Post
    1) Represent each integer as the largest integer type available like you are doing now.

    In order to get each of the digits separately write a loop that extracts number%10 (the last digit) and then divides the number by 10 up to the point where you are left with no digits left.
    I don't get the bit where you said "extracts number%10 (the last digit)" ... what does '%10' represent?

    Thanks

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    x%10 read "X modulo 10" is the remainder of dividing X by 10, thus the last digit of the number. Read more about module online.

  15. #15
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    What have you tried so far?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-16-2006, 01:43 PM
  2. formatting large drives
    By Shadow in forum Tech Board
    Replies: 1
    Last Post: 03-18-2006, 04:39 AM
  3. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  4. Formatting Standards
    By subdene in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 11-22-2002, 04:36 PM
  5. formatting dos game
    By Jhanoosh in forum Game Programming
    Replies: 1
    Last Post: 11-18-2002, 07:26 PM

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