Function returns different results than isolated program

I'm working on an assignment where the program must find the Decimal and Binary equivalent of a single Hexadecimal digit.
Hex -> Decimal works fine, however Hex -> Binary results incorrectly.
I tried and isolated the code for the Binary code, which works, but when used as a function, results in different numbers.

Isolated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using namespace std;
int main()
{
   int decimal, binary = 0, modCount = 8;
   double binCount = 1000;
   cin >> decimal;
   
   while(binCount != 0.1)
   {
      
      if((decimal / modCount) >= 1)
      {
         binary += binCount;
      }
      decimal %= modCount;
      modCount /= 2;
      binCount /= 10;
   }
   cout << binary;
}

This works as expected for inputs 0-9 & a/A-f/F, which is all thats needed.

Whole 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using namespace std;
int decimalConverter(int hexInput)
{      
   int decimal;
   if(hexInput >= 48 && hexInput <= 57)
   {
      decimal = hexInput - 48;
      return decimal;
   }
   else if(hexInput >= 65 && hexInput <= 70)
   {
      decimal = hexInput - 55;
      return decimal;
   }
   else if(hexInput >= 97 && hexInput <= 102)
   {
      decimal = hexInput - 87;
      return decimal;
   }
}

int binaryConverter(int hexInput)
{
   int binary = 0, modCount = 8;
   double binCount = 1000;
   
   while(binCount != 0.1)
   {
      
      if((hexInput / modCount) >= 1)
      {
         binary += binCount;
      }
      hexInput %= modCount;
      modCount /= 2;
      binCount /= 10;
   }
   return binary;
}

int main()
{
   int hexVal = 0;
   char hex;
   cout << "Enter a single hexadecimal digit. (Only the first character is read):  ";
   cin >> hex;    //values of 0-9 are 48-57. A-F is 65-70. a-f is 97-102
   cin.sync();
   hexVal = hex;  //converts char to "readable" numbers
   
   for(int x = 1; x == 1;)
   { 
      if(hexVal >= 48 && hexVal <= 57)
      {
         cout << "Hexadecimal:" <<setw(3) << hex
              << "\nDecimal:" <<setw(7) << decimalConverter(hexVal)
              << "\nBinary:" <<setw(8) <<setprecision(4) << binaryConverter(hexVal);
         break;
      }
      else if(hexVal >= 65 && hexVal <= 70)
      {
         cout << "Hexadecimal:" <<setw(3) << hex
              << "\nDecimal:" <<setw(7) << decimalConverter(hexVal)
              << "\nBinary:"  <<setw(8) << binaryConverter(hexVal); 
         break;
      }
      else if(hexVal >= 97 && hexVal <= 102)
      {
         cout << "Hexadecimal:" <<setw(3) << hex
              << "\nDecimal:" <<setw(7) << decimalConverter(hexVal)
              << "\nBinary:" <<setw(8) << binaryConverter(hexVal);
         break;
      }
      else
      {
         cout << "Invalid number. Enter a single hexadecimal digit:  ";
         cin >> hex;
         cin.sync();
         hexVal = hex;
      }
   }
   
   return 0;

}


Input: 1

1001


Input: 2

1010


Input: a

1001


Any help would be appreciated, thanks.
Last edited on
You are passing the character code (hexVal) to binaryConverter so when you enter 1 as input it passes 49 instead of 1.
oh wow.
I can't believe i missed that.

fixed it and it works fine now, thank you.
1
2
3
4
         cout << "Hexadecimal:" <<setw(3) << hex
              << "\nDecimal:" <<setw(7) << decimalConverter(hexVal)
              << "\nBinary:" <<setw(8) <<setprecision(4) << binaryConverter(decimalConverter(hexVal));
         break;
Topic archived. No new replies allowed.