Hex addition program.

I am writing code to take input and then convert it from decimal form to hexadecimal form, then add said hex numbers and display them. I know that what i have is borderline rubbish and I am still revising it but, I really need some input. Also I am using Microsoft Visual Studio as an editor, however I also have code blocks installed if theirs something that will conflict.

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

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <string>

using namespace std;

void calculations(int[]);
void hex_conversions(int[], string, string);
void num_corrections(string, string);


int main() {
	string input_dec1, input_dec2;
	int converted_hex[2];
	char change;

	cout << "Please input the two numbers you would like to add:" << endl;
	cout << "Please enter number 1:";
	cin >> input_dec1;
	cout << endl << "Please enter number 2:";
	cin >> input_dec2;
	cout << endl;

	cout << "The two numbers you are adding are " << input_dec1 << " and " << input_dec2 << "." << endl;
	cout << "Would you like to change them? (Y/N)" << endl;
	cin >> change;

	if (change == 'y' || change == 'Y')
		num_corrections(input_dec1, input_dec2);
	else
		hex_conversions(converted_hex, input_dec1, input_dec2);

	//    calculations(converted_hex);

	return 0;
}


void num_corrections(string input_dec1, string input_dec2) {
	int counter = 1;

	cout << "Please enter the new numbers you want to add:" << endl;
	cout << "Re-enter number 1:";
	cin >> input_dec1;
	cout << endl << "Re-enter number 2:";
	cin >> input_dec2;
	cout << endl;



	cout << "The two numbers you are adding now are, " << input_dec1 << " and " << input_dec2 << "." << endl;
}


void calculations(int converted_hex[]) {
	int anwser;

	anwser = converted_hex[0] + converted_hex[1];

	cout << "The two numbers added together are equal to " << anwser << " in hexadecimal form." << endl;
}


void hex_conversions(int converted_hex[], string input_dec1, string input_dec2) {
	int size1 = size(input_dec2) - 1;
	int size2 = size(input_dec1) - 1;

	for (int i = 0; i<size1;i++) //to change the first number to hex format.
		switch (input_dec1) {
		default:
			cout << "Please check your numbers again." << endl;
			num_corrections(input_dec1);
			break;
		case '0':
			converted_hex[i] = 0;
			break;
		case '1':
			converted_hex[i] = 1;
			break;
		case '2':
			converted_hex[i] = 2;
			break;
		case '3':
			converted_hex[i] = 3;
			break;
		case '4':
			converted_hex[i] = 4;
			break;
		case '5':
			converted_hex[i] = 5;
			break;
		case '6':
			converted_hex[i] = 6;
			break;
		case '7':
			converted_hex[i] = 7;
			break;
		case '8':
			converted_hex[i] = 8;
			break;
		case '9':
			converted_hex[i] = 9;
			break;
		case 'a':
			converted_hex[i] = 10;
			break;
		case 'A':
			converted_hex[i] = 10;
			break;
		case 'b':
			converted_hex[i] = 11;
			break;
		case 'B':
			converted_hex[i] = 11;
			break;
		case 'c':
			converted_hex[i] = 12;
			break;
		case 'C':
			converted_hex[i] = 12;
			break;
		case 'd':
			converted_hex[i] = 13;
			break;
		case 'D':
			converted_hex[i] = 13;
			break;
		case 'e':
			converted_hex[i] = 14;
			break;
		case 'E':
			converted_hex[i] = 14;
			break;
		case 'f':
			converted_hex[i] = 15;
			break;
		case 'F':
			converted_hex[i] = 15;
			break;
		}

	for (int i = 0; i<size2;i++) //To change the second input to the respective hex format
		switch (input_dec1) {
		default:
			cout << "Please check your numbers again." << endl;
			num_corrections(input_dec1);
			break;
		case '0':
			converted_hex[i] = 0;
			break;
		case '1':
			converted_hex[i] = 1;
			break;
		case '2':
			converted_hex[i] = 2;
			break;
		case '3':
			converted_hex[i] = 3;
			break;
		case '4':
			converted_hex[i] = 4;
			break;
		case '5':
			converted_hex[i] = 5;
			break;
		case '6':
			converted_hex[i] = 6;
			break;
		case '7':
			converted_hex[i] = 7;
			break;
		case '8':
			converted_hex[i] = 8;
			break;
		case '9':
			converted_hex[i] = 9;
			break;
		case 'a':
			converted_hex[i] = 10;
			break;
		case 'A':
			converted_hex[i] = 10;
			break;
		case 'b':
			converted_hex[i] = 11;
			break;
		case 'B':
			converted_hex[i] = 11;
			break;
		case 'c':
			converted_hex[i] = 12;
			break;
		case 'C':
			converted_hex[i] = 12;
			break;
		case 'd':
			converted_hex[i] = 13;
			break;
		case 'D':
			converted_hex[i] = 13;
			break;
		case 'e':
			converted_hex[i] = 14;
			break;
		case 'E':
			converted_hex[i] = 14;
			break;
		case 'f':
			converted_hex[i] = 15;
			break;
		case 'F':
			converted_hex[i] = 15;
			break;
		}
}

//End code
Last edited on
You forgot the [/code] part at the end of your code.
http://www.cplusplus.com/articles/jEywvCM9/

Considering you've already included the string library, why not use stoi?
http://www.cplusplus.com/reference/string/stoi/
Thank you, two things, the first being, what is stoi? The second the teacher that I will be submitting this program to requires that we only use what he has used in class.
what is stoi?
string to int
Last edited on
Topic archived. No new replies allowed.