I am writing a program to convert a base 10 integer to any base 2-16. Here are the terms:
"The method is to convert the decimal value the user selected into whatever base the user selected and print the converted value one place value at a time by using an index into an array of characters 0-9 amd A-F that is initialized to contain those characters.
In the method you will find the largest place value (i.e. power of the base) that will divide into the decimal number.
Then you can set up a loop that will operate from that power down to and including the 0th power to determine how many times each place value goes into the decimal number. Using the loop counter, index into the character array to print the character that corresponds to the quotient number and then subtract the product of the place value and the quotient from the decimal number. With the power (loop index) decreased, repeat until you finish the 0th place value.
Here's what I have so far:
I think I have it set up correctly, I'm just not sure how to attack it. I understand the concept of what I'm doing, just not the execution of it.
You might find it useful to work through some examples yourself, just with pencil and paper. For example you could take the number 3907 and convert it to base 7 using the method you described there. (Which isn't the best method, but that doesn't matter because you have to use the method you described.) Try several different examples, just picking random inputs and bases. Make sure you watch what you're doing and try to match it to the code you have there.
And don't be too attached to that code. If you find it doesn't correspond to what your pencil is doing, then don't hesitate to toss it out and replace it with something more suitable.
comments like "end main" are really not needed. If you use proper indentation, it is obvious where each method/loop ends and you don't have the clutter of those comments. Check this out:
It's easy to tell at a glance where things start and stop. also, proper formatting makes it easier to find the extra curly brace you have in your code.
Having that extra brace tells me you are not compiling often enough. NEVER write more than 2-3 lines of code before you re-compile. It is much easier to find and fix a problem if you've added 3 lines than if you've added 30.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Another bit of good advice, but very counter‑intuitive.
You will find your indentation and brace matching much easier of you get a decent text editor (eg gedit, kate, NotePad2, jEdit, NotePad++) and select its options to include:-
1: Bracket matching.
2: Syntax highlighting (you can probably change the colours to suit yourself).
5: Right margin warning at not more than 120 characters
The NotePad program which comes with Windows® is a very poor tool for programming, because it does not support those options (or didn’t when I last tried it), and has some nasty habits like adding .txt to files while you are not watching.
Once you have those features working, the most counter‑intuitive thing of all, is that you write much of your code backwards