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?
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?
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
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).
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
Re: inserting a comma on the display
Quote:
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
Re: inserting a comma on the display
There are no rules except using the % operator.
Re: inserting a comma on the display
Quote:
Originally Posted by
Brian_Jones
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
Re: inserting a comma on the display
Quote:
Originally Posted by
Brian_Jones
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/
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.
Re: inserting a comma on the display
The setfill and setw manipulators are exactly what you need here.
Re: inserting a comma on the display
Quote:
Originally Posted by
Brian_Jones
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
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.
Re: inserting a comma on the display
Thanks all for your advice, it worked!