Hello again @Jennifer1181,
First, let me remind and illustrate again about code tags. You notice that when I posted some code it appears formatted in a panel. This is what code tags do, and help us do a number of things to help posters.
Then next time you post code, put this before the text:
This is the "starting code tag"
Now, at the end of your code, you must close the tag with this:
The "slash" before the word code makes it closing tag for code.
You'll notice buttons on the right when you enter a post, and they automatic code tags, quote tags and formatting tags.
Let's start with this goal:
I was hoping to be able to have the user input a number no larger than 10 digits and then select two different bases for output.
|
You've taken in num, base and next_base and that works.
You've tested base to be in range. You may want to test base_next as well. I might go so far as to suggest that base_next may also be zero (which would mean don't bother with the second base display, perhaps).
Either:
1 2
|
if(base_next < 2 || base_next > 16)
throw out_of_range("Invalid base for conversion");
| |
Or perhaps, optionally
1 2
|
if( ( base_next < 2 || base_next > 16 ) && base_next != 0 )
throw out_of_range("Invalid base for conversion");
| |
This second option allows base_next to be zero without throwing the exception. base_next can't be 1, or > 16, but it can be zero if you want to use zero for the second base, to make it optional.
Now we come to this:
for (; num; num/= base; num/= next_base) {
result.insert(result.begin(), digits[abs(num % base)(num % next_base)]);
}
|
Which I'm sure you know isn't working. You won't be able to combine the calculation of the result for both bases in one loop. For any two bases the result may not be the same length, and so you will have to create TWO loops, not one. The example code I posted suggests one way.
However, I hinted that it may be better to make this a function. First, you must declare the function above main, like this:
|
void display_result( int num, int base, bool is_neg, string & digits );
| |
This would be placed just above "using namespace std;"
This function declaration is one entry in what becomes a kind of "table of contents", metaphorically, when you write larger programs.
Below your catch, you write the function. Something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void display_result( int num, int base, bool is_neg, string & digits )
{
string result; // this is local to the function. it is new, and empty, each time the function executes
// this is your original for loop (not the new one).
for (; num; num/= base)
{
result.insert(result.begin(), digits[abs(num % base)]);
}
if (is_neg) result.insert(result.begin(), '-');
cout << result << '\n'; // this is the same output you used
}
| |
This function will produce output for as many bases as you like.
Now, in the main body of code, consider this section.
1 2 3 4 5 6 7 8 9
|
string result;
for (; num; num/= base; num/= next_base) {
result.insert(result.begin(), digits[abs(num % base)(num % next_base)]);
}
if (is_neg)
result.insert(result.begin(), '-');
cout << result << '\n';
| |
I'm suggesting you remove ALL of that.
Replace it with this:
1 2 3 4 5
|
display_result( num, base, is_neg, digits );
if ( base_next != 0 ) display_result( num, base_next, is_neg, digits );
| |
The display_result function does all of the work, twice (as required).