CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2009
    Posts
    40

    inserting a comma on the display

    I was given a good question and I can't figure it out. It says:

    Write a program that reads in an integer that is greater than 1,000 and less than 1,000,000. The number must be entered without a comma. Display the number with a comma inserted where it belongs. (Hint: use the % operator)

    How can this be solved?

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: inserting a comma on the display

    first two questions to answer for yourself.

    1. How does base 10 representations of numbers work?
    2. What arithmetical operation does % represent?
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Oct 2009
    Posts
    40

    Re: inserting a comma on the display

    base 10 numbers?
    10^3=1000
    1,000,000/1000=1000
    base 10 number- decimal system

    % represents the remainder... 9%2=1, 7%100=7, 30%3=0

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: inserting a comma on the display

    If the user input is greater than 1,000 and less than 1,000,000, the output will be something like xxx,yyy i.e. a part before the comma and a part after the comma.

    Think how to split up the original number into the part before and after the number.

    Learn how to format the part after the comma to have exactly 3 digits on output (e.g. 007).

    Output the three components (first number, comma character, second number).
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  5. #5
    Join Date
    Oct 2009
    Posts
    40

    Re: inserting a comma on the display

    If the number is 22202

    22202%1000=202 (looking good so far for the part after the comma)

    integer math, 22202/1000 gives 22 and that would be the part before the comma

    that breaks it up perfectly, but then if the input number is 30000
    the part after the comma is 30000%1000=0 and then I can't really make it work. right track I'm not sure

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: inserting a comma on the display

    integer math, 22202/1000 gives 22 and that would be the part before the comma
    The comma is always placed before the three rightmost digits. There is no exception to that rule if the number will be between 1,000 and 1,000,000 (non-inclusive).

    You didn't mention the rules. Are you allowed to use strings to represent the number (i.e. convert the number to a string and work with the string)?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2009
    Posts
    40

    Re: inserting a comma on the display

    There are no rules except using the % operator.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: inserting a comma on the display

    Quote Originally Posted by Brian_Jones View Post
    There are no rules except using the % operator.
    That will get you the digits preceding the comma, but to get the digits after the comma is just a matter of stringizing the number and getting the 3 rightmost characters.

    You'll need to use some sort of string manipulation to get the last three digits, since an integer 0 (in the case of 30000%1000), is 0. It isn't "000". So either you have to turn the number into a string, or on ouput, format the number to have leading zeros (up to 3 digits). I think this is where your stumbling block is, converting a true integer into a string representation of it, with whatever formatting you need to do. So it isn't the math that's the problem.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 2nd, 2009 at 09:35 AM.

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: inserting a comma on the display

    Quote Originally Posted by Brian_Jones View Post
    There are no rules except using the % operator.
    here is one option:
    Look into manipulators:
    http://www.cplusplus.com/reference/i.../manipulators/
    read the functions setfill and setw. The examples provided should be enough.

    Or do as Paul said: Transform your number into a string, and hack away! Since your number is a string, you know you want to insert the character ',' at position string.end()-3.
    hey look! http://www.cplusplus.com/reference/s...string/insert/

  10. #10
    Join Date
    Oct 2009
    Posts
    40

    Re: inserting a comma on the display

    well the last two posts might make it work too, but I have to take a step back to posts #2 and #4 because they deal with the % operator.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: inserting a comma on the display

    The setfill and setw manipulators are exactly what you need here.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: inserting a comma on the display

    Quote Originally Posted by Brian_Jones View Post
    well the last two posts might make it work too, but I have to take a step back to posts #2 and #4 because they deal with the % operator.
    Yes, use the mod operator to get the integer version of the last three digits. You have that covered already.

    Then what monarch_dodra and Lindley are saying is to take that integer and format it correctly using manipulators on output. It's this step that is the final step.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: inserting a comma on the display

    this problem has two parts.

    1. Use integer arithmetic operations to break a number into two numbers.
    One representing the part less than 1000 and one representing the part greater than 1000.

    2. Output the result of part 1 to represent the original number with a comma inserted.

    For part one given, a number 1000000 > X > 1000 what is the result of integer division by 1000 and the remainder operator by 1000.

    For part two io manipulators.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  14. #14
    Join Date
    Oct 2009
    Posts
    40

    Re: inserting a comma on the display

    Thanks all for your advice, it worked!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured