459,203 Members | 1,638 Online Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

 P: 5 Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: Expand|Select|Wrap|Line Numbers #include  using namespace std; void binary(int number)  {     int remainder;       if(number <= 1)      {         cout << number;         return;     }       remainder = number%2;     binary(number >> 1);         cout << remainder; }   void hexadecimal(int number) {     int remainder;     while (number >= 1)     {         remainder = number%16;         if (remainder > 9)         {             if (remainder == 10)                 cout << "A";             if (remainder == 11)                 cout << "B";             if (remainder == 12)                 cout << "C";             if (remainder == 13)                 cout << "D";             if (remainder == 14)                 cout << "E";             if (remainder == 15)                 cout << "F";         }         else         {             cout << remainder;         }         number = number/16;     } }; void octal(int number) {     int remainder;     int i;     int ten = 10;     remainder = number % 8;     number = number/8;     i = remainder;     while (number >= 1)     {         remainder = number%8;         number = number/8;         i += remainder * ten;         ten = ten * 10;     }     cout << i << "\n"; }; int main () {     int number;     cout << "Enter a number in decimal: ";     cin >> number;     int spec;     cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";     cin >> spec;     if (spec == 1)     {         binary (number);         cout << "\n";     }     if (spec == 2)         octal (number);     if (spec == 3)         hexadecimal (number);     return 0; }   Jul 27 '08 #1
14 Replies

 Expert Mod 5K+ P: 8,959 Instead of outputting to cout output to a string stream, then when you have finished reverse the string and output the string. In fact better still don't output to cout in you function at all, have you function create and return a string and output the string from you main function. Jul 27 '08 #2

 Expert 10K+ P: 11,448 I don't know why this thread was closed; another solution would be to simply reverse the recursive steps like this: Expand|Select|Wrap|Line Numbers void binary(int x) {    if (x > 1) binary(x>>1);    cout << (x&1); }   Instead of applying right (or 'tail') recursive steps use left recursion where you test for the sentinel condition first. kind regards, Jos Jul 27 '08 #3

 Expert Mod 5K+ P: 8,959 I don't know why this thread was closed; Sorry that was me, not enough coffee yet this morning, thank you for re-opening it. Jul 27 '08 #4

 Expert 10K+ P: 11,448 Sorry that was me, not enough coffee yet this morning, thank you for re-opening it. No need to apologize, I know that feeling; my recipe: two cups of original Italian espresso coffee in the morning, that'll reduce the amount of blood in the coffee veins. kind regards, Jos ;-) Jul 27 '08 #5

 P: 5 Ok so I have a decimal to hexadecimal converter and I want to use a stack class to reverse the characters I get into the proper order... Here's the code I have for the converter: Expand|Select|Wrap|Line Numbers void hexadecimal(int number) {     int remainder;     while (number > 0)     {         remainder = number % 16;         number = number/16;         if (remainder > 9)             cout << char('A'  + remainder - 10) ;         else cout << remainder;     } };   How would I make a stack class to reverse the order? Should I store the characters I get in an array? How would I do that? Thanks for all help, Greg Jul 29 '08 #6

 Expert 100+ P: 1,764 You could use STL's string for this. Instead of using cout use string's += operator to add a new char to the string.After that you can obtain string reverse iterator and print the string in reverse order. Take a look at this for reference to how to reverse a string. Jul 29 '08 #7

 Expert Mod 5K+ P: 8,959 Please do not start multiple threads on the same topic, it is against the site posting guidelines, please read them. Banfa Administrator Jul 30 '08 #8

 Expert 10K+ P: 11,448 Did anyone actually *read* my reply #3? There is no need to reverse anything at the end. kind regards, Jos Jul 30 '08 #9

 Expert Mod 5K+ P: 8,959 Did anyone actually *read* my reply #3? There is no need to reverse anything at the end. I did Jos but it is re-working the binary function and unless I missed something is actually functionally equivalent to what is already there although more elegant, however the OP asked for help in the hexadecimal function. Jul 30 '08 #10

 Expert 10K+ P: 11,448 I did Jos but it is re-working the binary function and unless I missed something is actually functionally equivalent to what is already there although more elegant, however the OP asked for help in the hexadecimal function. I don't understand that; there is nothing structurally different between a binary() or hexadecimal() function; both can use that left recursion (see reply #3) so nothing needs to be reversed. Basically they all go: if (n > radix) recurse(n/radix); print representation of n%radix; kind regards, Jos Jul 30 '08 #11

 Expert Mod 5K+ P: 8,959 I don't understand that; there is nothing structurally different between a binary() or hexadecimal() function; both can use that left recursion (see reply #3) so nothing needs to be reversed. Basically they all go: if (n > radix) recurse(n/radix); print representation of n%radix; I agree entirely in principle but that isn't how the hexadecimal function was written initially. My comments suggest how to fix it without introducing recursion based on what was already there. Actually now I think about it a recursion of this sort is probably one I would be happy to put into a live project. My normal worry with recursion is preventing or not risking stack overflow which can be troublesome if there is no well defined boundary to the limit of recursion. For a solution to this particular problem the boundary is very well defined because of the limiting fact in the maximum value an unsigned integer can hold. Jul 30 '08 #12 