Decimal to Hexadecimal Conversion

Hi everyone! First of all, I am completely new to programming so I apologize in advance for any major mistakes.

So I've been assigned to write a program that converts decimal to hexadecimal, and the instructions say that I may not use any of the dec or hex format flags provided by iostream or the stoi, stol, stoul,stoll, stoull, stof, stod, and stold functions provided by the string library.

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <iostream>
#include <string>
using namespace std;

int main() {

  int num;
  int division = 0;
  int remainder = 0;
  string rem = "";
  string hex_num = "";



  cout << "Enter a decimal number: " << endl;
  cin >> num;

  while (division == 0)
  {
    division = num / 16;
    remainder = num % 16;
    {
      if (remainder > 10) //if remainder is greater than 10
      {
        char c = static_cast <char>((remainder - 10) + 'A'); // subtract 10 from division and add 'A' (65) to get character in ASCII (A-F)
        rem = string(1, c); // convert char into a string of length 1
        cout<<rem<<endl;
      
      }
      else if (remainder == 10)//remainder is equal to 10
      {
        char c = static_cast <char>(17 + '0'); //add 17 to get character 'A'
        rem = string(1, c);
        cout << rem << endl;
      }
      else // remainder is less than 10
      {
        char c = static_cast <char>(remainder + '0'); // Add remainder to '0' (48) to get character in ASCII (0-9)
        rem = string(1, c); // convert char into a string of length 1
        cout << rem << endl;
      }

    }
  }
  hex_num = //string sum of remainder characters

  cout << "Your number in hex is 0x" << hex_num << endl;
}


I think it's my while loop that I'm not getting right, and I am also having trouble with coming up with how to concatenate the characters into a string that displays the hexadecimal. Any help/hints would be greatly appreciated. Thanks!
while (division == 0)

This doesn't seem to make a lot of sense.

division = num / 16;, so if the number is 160, division will be set to ten, and you'll only go round the while loop once.
It's hard to answer without saying too much.

Regarding the while loop, my suggestion,
1
2
3
4
5
    int num;
    cout << "Enter a decimal number: " << endl;
    cin  >> num;    
        
    int division = num;


Then the loop condition is while (division != 0)

Some of the other code is more complex than necessary, the code for remainder == 10 can be merged with code for remainder > 10, they both have the same requirements.

To build the final string, you need to concatenate (join) the character c to the string hex_num. Add it to the start, not the end, using the + operator.

http://www.cplusplus.com/reference/string/string/operator+/
@Repeater Yeah at first it didn't make sense to me either. I asked someone else to take a look at my while loop and they suggested to set the condition to division==0, the thought process behind that being if, for example, the user inputs the number 177:

division=(177)/16 //equals 11
remainder= (177)%16// equals 1,

Then loop back since division is not yet 0:

division=11/16 //equals 0
and since division is equal to 0, it terminates the loop.

Maybe I understood wrong to be honest, but this was what they were getting at.
@Chervil

Ah okay, thank you! I made those changes :)
Topic archived. No new replies allowed.