Help needed. Simple C++ program.

This is a discussion on Help needed. Simple C++ program. within the C++ Programming forums, part of the General Programming Boards category; How do i make a program using functions to get a "long" type number from the user and display it ...

Help needed. Simple C++ program.

How do i make a program using functions to get a "long" type number from the user and display it back to the screen with commas to seperate the digits.

For example, if the user puts in a "1982633", then it should print back to the screen, "1,982,633". It should do this for any number up till 10 digits. '
It should be simple, basic and easy to understand for i am a newbie and i dont know C++ very well.

figure a count method for how long the number the user is entering is, then by doing so place a comma every 3 digits.

Would you like to show us how you plan to output commas in the middle of a long?

It's do-able, but it might be a tad complex for a newbie. I've done it before so I can help you out. The logic is quite simple once you see it in action, but it might take a while to get used to the applications of some of the lesser-used concepts, like modulus (remainder)You'll need to convert the number to an array of chars first, but I don't know of any function that will do this for you (of course you can go the other way with cstdlib). I suggest you use a loop and the modulus operator (think, "10's place, 100's place, etc..." - PM me if you need some more help), and you can fill in the digits one by one. Once you've done that, start at the end of your array and work toward the beginning. Into a new, larger array, read in 3 digits from your old array, then a comma, and repeat until you can get to the beginning of your old array in less than 3 jumps.

try writing it out first - find out in detail what it is you really need to do here... for example, you know that you want to seperate the numbers into three sections and put commas in between those three sections. that's the obvious part.

now you have to think more in-depth about it. computers can do math billions of times faster than you, but you can recognize most patterns billions of times faster than a computer can. case in point: breaking up a number into three sections.

now you can do this several ways. either way, you want to start at the righ side (ones place) of the number and count off by threes moving to the left until you have less than three left. again, this is what you need to figure out.

you need to come up with an algorithm, or a method of doing something. for example, say I want to swap two numbers. I can just say to 'swap' them and it's that simple. but in computer terms, you need to come up with an algorithm to do that. in this case, it would go something like this:

create a third temporary position

put item one in that position

put item two in item one's position

put the item in the temporary position in position two

now it's up to you to do something like that for your situation. again, you know you need to segment the number into sections of three. I recommend you look into the use of the mod operator (%), strings and/or arrays.

I can't find another way to make it so that the indenting is only 1 level to the right. I'm attemping to use Siewald's "Pillars of Pretty Code" method. He says to use goto if needed, and it seemed needed. I could use a loop, but it would mean my code would be flushed more to the right, and that's bad for the code structuring I'm trying to attempt.

Kleid-0, I don't care that you're posting solutions dude, but you're teaching bad habits - stop using goto's, or at least, in the beginning of your post, worn the op that you are using poor coding style.

>>I can't find another way to make it so that the indenting is only 1 level to the right.
Hm? If you're trying not to have any indentation, loops aren't going to force you to indent. It's just about the same readability if you use a loop without indentation as if you use goto to avoid indentation. Heck, if I was forced to use goto as a loop mechanism like that, I'd indent it anyway just to make it clear that that particular block of code goes in one piece.

In short, I think Mr. Seiwald is an idiot, unless he's a renegade sadist who enjoys destroying the lives of maintenance programmers, or unless he's writing about techniques for obfuscated code contests. In which case he's a freaking genius.

>>Rearrange conditionals so that the block with the quickest exit comes first....
That's got nothing to do with goto.

If you're trying not to have any indentation, loops aren't going to force you to indent.

Yes but it's customary and higher readability if you do indent on loops.

The whole goal of Mr Seiwald's Pretty Code on page 63 of the care bears magazine is that TOO much indentation is going to eat out our brains and we're all going to explode!! I don't care if this is the Nazi party, but we must follow together as a family! We need our code to be as readable as possible, and goto was required by state law.