Char 48 ("0") doesn't appear.

I'm trying to make a program that converts decimal values to hexadecimal values. I've gotten all the processing and everything taken care of, but I have a problem where 0's don't seem to show up unless there's something after them. I'm using loops and the same variable as each digit.
@COM314

The problem you're having is probably in the coding. Show the section of programming that does the converting from decimal to hex values, and someone will be able to point you in the correct direction to a working program.
A blind, probably wrong guess, at some point you have a c-string that you have inadvertantly null terminated:
1
2
3
4
5
6
7
8
9
10
11
12
char *number = "109"

for (int i = 0; i < strlen(number) ; i++)
{
  number[i] -= '0';
}

int result = 0;
for (int i = 0; i < strlen(number) ; i++)  // Logic error, strlen(number) is now 1 
{
   result += number[i] * pow(10, i); 
}
Here's the entire Program

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
int d, h, b, s, n, p;
char o;
n = 1;
while(n == 1)
{
p = 1;
cout << "Provide decimal value: ";
cin >> d;
cout << endl;
s = d;
while((d / p) >= 16)  // This loop determines the highest place.
{
p = p * 16;
};
cout << "Hexadecimal equivalent: ";
while(s > 0)  // This loop determines each digit and outputs it sequentially from largest to smallest.
{
h = int(s / p);
s = s % p;
p = p / 16;
if(h < 10)
o = h + 48;
else if(h >= 10 && h <= 16)
o = h + 55;
else
cout << "Error";
cout << o;
};
cout << endl << endl << endl;
};
}
The while loop at line 24 is key here.
This condition while(s > 0) can be read as "while at least one of the remaining digits is non-zero". That is the precise cause of the symptoms you are getting.

If you're not sure what is happening, use a debugger, or insert additional cout statements to display some or all of the key variables from within the loop.
If line 35 cout << o; is temporarily replaced with this debugging code,
35
36
    //cout << o;
    cout << "\nh: " << h << "  s: " << s << "  p: " << p << "  o: " << o;

We get this output:
Provide decimal value: 257

Hexadecimal equivalent:
h: 1  s: 1  p: 16  o: 1
h: 0  s: 1  p: 1  o: 0
h: 1  s: 0  p: 0  o: 1
Provide decimal value: 256

Hexadecimal equivalent:
h: 1  s: 0  p: 16  o: 1

Notice the value of "s" in each case. In the second, where the input was 256, s becomes zero on the first iteration, and hence just a single digit is output.

However, what is more interesting is the value of p. Notice in the first case (input 257, which worked), p becomes zero at the same stage as s.

What is required is to go around the loop until the lowest-order digit has been output. Luckily in this case the solution is really simple. Change the condition at line 24 to
while (p > 0)

(and take out the debugging code of course).
... Of course. I should have thought of that. Thank you very much. It works perfectly now. :D
Topic archived. No new replies allowed.