Thread: Formatting with hyphens

  1. #16
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    How are you going to write a C program without a main() function?
    Mainframe assembler programmer by trade. C coder when I can.

  2. #17
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    How would anyone check if the program worked if you can't use IO functions from the standard library or self-written (you wouldn't be able to output to a file or a terminal window, or anything)?

    If it just has to be a C program, then you could make a large comment with several underscores and hyphens between them. Then have the user input the number into the comment where each blank is. It's kind of cheating because the text editor is doing the work, but that's the only solution I can think of.
    Last edited by TieFighter; 03-12-2010 at 08:52 PM.

  3. #18
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    You can use the following code, if you accept the input type as character array. Then the following code will work fine.
    Code:
    #include<stdio.h>
    main()
    {
            char number[15];
            printf("Enter the 10 digit numbers\n");
            scanf("%s",number);
            int a,b,c;
            sscanf(number,"%3d%3d%4d",&a,&b,&c);
            printf("%d-%d-%d\n",a,b,c);
    }
    Last edited by murugaperumal; 03-12-2010 at 09:47 PM.

  4. #19
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by murugaperumal View Post
    You can use the following code, if you accept the input type as character array. Then the following code will work fine.
    Code:
    #include<stdio.h>
    main()
    {
            char number[15];
            printf("Enter the 10 digit numbers\n");
            scanf("%s",number);
            int a,b,c;
            sscanf(number,"%3d%3d%4d",&a,&b,&c);
            printf("%d-%d-%d\n",a,b,c);
    }
    He is not allowed to use strings so that won't work.

  5. #20
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    You can use the following code. But you need get the input as string.
    Code:
     
    #include<stdio.h>
    main()
    {
             int  number;
            printf("Enter the 10 digit numbers\n");
            scanf("%d",&number);
            int a,b,c;
            sscanf(number,"%3d%3d%4d",&a,&b,&c);
            printf("%d-%d-%d\n",a,b,c);
    }

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by murugaperumal View Post
    You can use the following code. But you need get the input as string.
    That doesn't even make sense.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    I have merged Frost45's and riders29's questions into this thread and renamed this thread "Formatting with hyphens".

    Based on what appears to be the ridiculous restriction on what you people can or cannot do, I am inclined to say that claudiu has the right idea: this is really a question about using arithmetic operations to break an integer in decimal representation into its constituent digits.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Quote Originally Posted by Mini View Post
    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.
    Hey are you in Achim's class?

  9. #24
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by clchen View Post
    Hey are you in Achim's class?
    Sorry. What's that?

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It creates a char (string) array named "numbers", with up space for up to 15 char's.

    Apparently it's hard to get the message across that you can't use char's in your assignment.

    So are you guys limited to things like:

    number = 987654321

    num1 = number % 10
    number /= 10

    num2 = number % 10
    number /= 10

    num3 = number % 10

    etc. until number == 0

    ?
    Last edited by Adak; 03-16-2010 at 01:54 AM.

  11. #26
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by Adak View Post
    number = 987654321

    num1 = number % 10
    number /= 10
    What exactly would this do? I'm lost

    EDIT: I know what this does now... But I'm still lost as to how I can get one digit at a time.

    Eg. If I had 12345 and I wanted '1' to be stored in 'a' , '2' in 'b' , '3' in 'c' .. etc .. how would I do this? Thanks alot!

    EDIT 2: Actually I may know how to do what I've described above. If I'm still having troubles I'll post later. Thanks for all your help!!
    Last edited by Mini; 03-16-2010 at 03:23 AM.

  12. #27
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Quote Originally Posted by Mini View Post
    Sorry. What's that?
    Oh sorry, just from the question you posted and the number 9300605048577, I assumed you were in my computing class.

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