not converting from hex to decimal

okay so ive been working on this code for a while yet i can cant get it to work properly. The code inputs everything correctly yet will not convert the char hex number to a decimal number.
any help would be greatly appreciated and sorry for the long code

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

#include <iostream>
#include <string>

using namespace std;//the names defined in iostream are to be interpreted in the standard way.

void addTwoHexDigits(const char firstAddend, const char secondAddend, char& sumDigit, char& carryDigit);
										
void addTwoHexadecimalNumbers(const char addendOne[], const char addendTwo[], char total[], const unsigned int size);

char convertHexDigitToDecimalNumber(const char digit);

int main()
{
	 char firstAddend;
	 char secondAddend; 
	 char answer;
	 char addendOne[10];
	 char addendTwo[10];
	 char total[10];
	 int size = 2; 
	 char sumDigit;
	 char carryDigit;
	 int i;
	 char firstHexNumber[10];
	 char secondHexNumber[10]; 


	 do
	 {

		cout << "Input value for the first digit/letter:\n";
		for( i = 0; i < (size); i++)
		{
			cin >> firstHexNumber[i];
			firstHexNumber[i] = convertHexDigitToDecimalNumber(firstHexNumber[i]);
		}


		cout << "Input value for the second digit/letter:\n";
		for( i = 0; i < size; i++)
		{
			cin >> secondHexNumber[i];
			secondHexNumber[i] = convertHexDigitToDecimalNumber(secondHexNumber[i]);
		}

		for( i = 0; i < size; i++)
		{
			firstHexNumber[i] = convertHexDigitToDecimalNumber(firstHexNumber[i]);
			secondHexNumber[i] = convertHexDigitToDecimalNumber(secondHexNumber[i]);
		}

		for (i = 0; i < size; i++)
		{
			firstAddend = firstHexNumber[i];
			secondAddend = secondHexNumber[i];
			addTwoHexDigits(firstAddend, secondAddend, sumDigit, carryDigit);
		}

		addTwoHexadecimalNumbers(addendOne, addendTwo, total, size);


		cout << "Do you wish to run the program again? (Y/N):\n";
		cin >> answer;
	 }while ((answer == 'Y') || (answer == 'y'));

	system("pause");
	return 0;

}

void addTwoHexadecimalNumbers(const char addendOne[], const char addendTwo[], char total[], const unsigned int size)// viod function adds two hexadecimal numbers
{
	char sumDigit;// sumdigit will retuen a number or letter 
	char carryDigit = '0'; // carry digit starts off as zero when no calculations are done
	char sum_temp; //holds the value of the sum of sum 
	char carry_temp; // char variables used to hold the sum digit and carry digit
	int i;// used in for loop


	for (i = (size - 1); i >= 0; i--)// conditions for for loop 
	{
		if (carryDigit == '1')// if carry digit is 1 runs if statement
		{
			carry_temp = carryDigit;
			addTwoHexDigits(carry_temp, addendOne[i], sumDigit, carryDigit);
			sum_temp = sumDigit;
			addTwoHexDigits(sum_temp, addendTwo[i], sumDigit, carryDigit);
			total[i] = sumDigit;
		}
		else
		{
			addTwoHexDigits(addendOne[i], addendTwo[i], sumDigit, carryDigit);
			total[i] = sumDigit;
		}
		
	}

	if (carryDigit == '1') // if carry digit is equal to '1', then
	{
		cout << "Overflow error : ";
		for (i = 0; i < size; i++)
		{
			cout << "*" << endl;
		}

	}
	else
	{
		cout << endl << "The sum is: ";
		for (i = 0; i < size; i++)
		{
			cout << total[i] << endl;

		}
	}
	return;
	
}


void addTwoHexDigits(const char firstAddend, const char secondAddend, char& sumDigit, char& carryDigit)//void function that will calculate the value of the hexadecimal input buy running  
										  															  //converted values through a loop up chart and then dispaly the sum and carry over number
{
	char sumDigitTable[16][16] = //look up chart that looks up the place of input and then returns that output to the sumDigit 
	{
		{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'},
		{'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0'},
		{'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1'},
		{'3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2'},
		{'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3'},
		{'5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4'},
		{'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5'},
		{'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6'},
		{'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7'},
		{'9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8'},
		{'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
		{'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A'},
		{'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'},
		{'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'},
		{'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D'},
		{'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'},
	 };
	 char carryDigitTable[16][16] = //look up chart that looks up the place of the carry digit and then returns that output to the carryDigit
	{
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
	 };


	sumDigit = sumDigitTable[firstAddend][secondAddend];// returns value of the imputs after converting it to hexadecimal 

	carryDigit = carryDigitTable[firstAddend][secondAddend];// returns the carry overr number 

	

}


char convertHexDigitToDecimalNumber(const char digit)
{
    // convert the hex digit parameter into its decimal equivalent ('1' becomes 1, 'A' becomes 10, etc.)

    int digitValue;
    char i;

    switch (digit)
    {
    	case ('0'):
    	  digitValue = 0;
    	  break;
    	case ('1'):
    	  digitValue = 1;
    	  break;
    	case ('2'):
    	  digitValue = 2;
    	  break;
    	case ('3'):
    	  digitValue = 3;
    	  break;
    	case ('4'):
    	  digitValue = 4;
    	  break;
    	case ('5'):
    	  digitValue = 5;
    	  break;
    	case ('6'):
    	  digitValue = 6;
    	  break;
    	case ('7'):
    	  digitValue = 7;
    	  break;
    	case ('8'):
    	  digitValue = 8;
    	  break;
    	case ('9'):
    	  digitValue = 9;
    	  break;
    	case ('A'):
    	  digitValue = 10;
    	  break;
    	case ('B'):
    	  digitValue = 11;
    	  break;
    	case ('C'):
    	  digitValue = 12;
    	  break;
    	case ('D'):
    	  digitValue = 13;
    	  break;
    	case ('E'):
    	  digitValue = 14;
    	  break;
    	case ('F'):
    	  digitValue = 15;
    	  break;
    	default:
    	  digitValue = -1;	// parameter out of range
    	  
    } // end switch
    
    return digit;
    
}

On line 236 you return digit (your conversion has no effect), it's supposed to digitValue.

The return type of convertHexDigitToDecimalNumber() is supposed to be int

[EDIT]You do not output the decimal number
Last edited on
so i made changes to the code like you recommended but the issue is when its suppose to change the value of a to 10 for the look up table its not doing that, instead it shoot out some random character.
so, show your modified code.

the loop on line 47 converts an already converted value.
remove that loop.

on line 60: addendOne and addendTwo are not initialized

the loop on line 53 has no effect. I guess that you want to feed the two arrays for line 60?
on line 57: you do not add hex digits. They are already converted to decimal
Why are you converting between the two? There is no real reason except to output, and then you can have it automatically converted by the stream.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main() {

	unsigned my_decimal = 100;
	unsigned my_hex  = 0x0F;

	cout << "Decimal: " << my_decimal << " as Hex: 0x" << std::hex << my_decimal << endl;
	cout << "Hex: 0x" << my_hex << " as Decimal: " << std::dec << my_hex << endl;

	return 0;
}
well i need to basically convert my char charaters to integers. what that does is it allows the program to use the look up table to find value. so for example i want to add A and 1. it will convert A to 10 and use the look up table to see where 10 and 1 cross which is B. As to why, its because the professor wanted it this way.

sorry for the late replies.

below is a code that i wrote that works for one number but not a string of numbers.


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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
[#include <iostream>// a derective that tells the complier where to find information about certain items that are used in the program.
using namespace std;//the names defined in iostream are to be interpreted in the standard way.

void addTwoHexDigits(const char firstDigit, const char secondDigit, char& sumDigit, char& carryDigit);// tkes the input values runs it through the array tables and the n
																									  // outputs the addition of the two arrays 
int row_index_sum(char Digit);// converts first char input to type int

int main()//the names defined in iostream are to be interpreted in the standard way.
{// begining of program.

	 char firstDigit;// char since the input will be run through an array table that has char values 
	 char secondDigit;//char since the input will be run through an array table that has char values 
	 char sumDigit;// will add the two char values
	 char carryDigit;// will read the carry over from the addition of the char inputs 
	 char answer;//will store the users input for the do-while loop

	 do// do while loop
	 {// begining of do while loop body 
		cout << "Input value for the first digit/letter:\n";// asks the user to input the letter or number they wan to convert
		cin >> firstDigit;//inputs the value to the first digit 
		cout << "Input value for the the digit/letter:\n";// asks the user to input the letter or number they wan to convert
		cin >> secondDigit;// inputs the value into the second digit 

		firstDigit = row_index_sum(firstDigit);// return type function that will convert char input to character inputs 

		secondDigit = row_index_sum2(secondDigit);// return type function that will convert char input to character inputs 

		cout<< endl;

		addTwoHexDigits(firstDigit,secondDigit,sumDigit,carryDigit);// void function that will calculate the value of the hexadecimal input buy running converted 
																	//values through a loop up chart and then dispaly the sum and carry over number

		cout << "Do you wish to run the program again? (Y/N):\n";// asks user a question
		cin >> answer;// inputs users answer into nswer
	 }while ((answer == 'Y') || (answer == 'y'));// condition for the loop to return

	system("pause");// stops the program from colsing without user input.

	return 0;// end program when it gets here.

}// end of program.


void addTwoHexDigits(const char firstDigit, const char secondDigit, char& sumDigit, char& carryDigit)//void function that will calculate the value of the hexadecimal input buy running  
																									//converted values through a loop up chart and then dispaly the sum and carry over number

{
	char sumDigitTable[16][16] = //look up chart that looks up the place of input and then returns that output to the sumDigit 
	{
		{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'},
		{'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0'},
		{'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1'},
		{'3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2'},
		{'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3'},
		{'5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4'},
		{'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5'},
		{'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6'},
		{'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7'},
		{'9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8'},
		{'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
		{'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A'},
		{'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'},
		{'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'},
		{'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D'},
		{'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'},
	 };
	 char carryDigitTable[16][16] = //look up chart that looks up the place of the carry digit and then returns that output to the carryDigit
	{
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
		{'0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
	 };

	sumDigit = sumDigitTable[firstDigit][secondDigit];// returns value of the imputs after converting it to hexadecimal 

	carryDigit = carryDigitTable[firstDigit][secondDigit];// returns the carry overr number 

	
	cout << "The result is: " << sumDigit << endl;// output result to display
	cout << "Carry over is: " << carryDigit << endl;////outputs carry digit

	cout << endl;// outputs a blank line
}

int row_index_sum(char firstDigit)// converts hex numbers to decimal to use to look up values on the look up chart,char to int
{
	if (Digit == '1')
	{
		(Digit = 1);
	}
	else if (Digit == '2')
	{
		(Digit = 2);
	}
	else if (Digit == '3')
	{
		(Digit = 3);
	}
	else if (Digit == '4')
	{
		(Digit = 4);
	}
	else if (Digit == '5')
	{
		(Digit = 5);
	}
	else if (Digit == '6')
	{
		(Digit = 6);
	}
	else if (Digit == '7')
	{
		(Digit = 7);
	}
	else if (Digit == '8')
	{
		(Digit = 8);
	}
	else if (Digit == '9')
	{
		(Digit = 9);
	}
	else if (Digit == 'A')
	{
		(Digit = 10);
	}
	else if (Digit == 'B')
	{
		(Digit = 11);
	}
	else if (Digit == 'C')
	{
		(Digit = 12);
	}
	else if (Digit == 'D')
	{
		(Digit = 13);
	}
	else if (Digit == 'E')
	{
		(Digit = 14);
	}
	else if (Digit == 'F')
	{
		(Digit = 15);
	}
	return (Digit);
}
Last edited on
Topic archived. No new replies allowed.