writing integer values in char array elements

The program converts decimal to hexadecimal
1)I am having problems assigning integer values to char array elements how is it done. Im getting symbols for my array numerical values.
2) How do I initialize my array to spaces? to get rid of garbage and old data in array elements.
3) could someone explain what my problem is? with integers and chars?
4) Is cout part of my problem?
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

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>

int main()
{
	using namespace std;
	
	// binary declarations
	int x, i, z, n, remainder;
	int result = 0;
	bool loop_cond, remainder_found, octval_found;

	// octal declarations
	int octalval,octresult,octremainder,temp_octalval;
	int octloop_cnt;
	bool oct_loop;
	octresult = 0;
// hexadecimal declarations
	int hexval, hexremainder, temp_hexval;
	int hexloop_cnt;
	bool hex_loop,hexval_found;
	char hexresult[5] = {" "};

	cout << "decimal" << "\t" << "binary" << "\t"<< "octal"<<"\t"<<"hexadecimal"<< endl;

 for (i = 1; i <= 256; i++){  
hexloop_cnt = 0;
octremainder = 0;
hexval_found = false;
hex_loop = true;
hexloop_cnt = 0;
while (hex_loop != false){

	if (hexval_found != true){
		hexval = i / 16;
		hexremainder = i % 16;
	}
	else {
		temp_hexval  = hexval;
		hexval = hexval / 16;
		hexremainder = temp_hexval % 16;
	}


	if (hexval == 0){
		switch (hexremainder){
		case 10:
			hexresult[hexloop_cnt] = 'A';
			break;
		case 11:
			hexresult[hexloop_cnt] = 'B';
			break;
		case 12:
			hexresult[hexloop_cnt] = 'C';
			break;
		case 13:
			hexresult[hexloop_cnt] = 'D';
			break;
		case 14:
			hexresult[hexloop_cnt] = 'E';
			break;
		case 15:
			hexresult[hexloop_cnt] = 'F';
			break;
		default:
			hexresult[hexloop_cnt] = (char)hexremainder;
			break;
		}//end switch
		hex_loop = false;
	}//end if

	if ((hexval < 16) && (hexval > 0)){
		switch (hexremainder){
		case 10:
			hexresult[hexloop_cnt] = 'A';
			break;
		case 11:
			hexresult[hexloop_cnt] = 'B';
			break;
		case 12:
			hexresult[hexloop_cnt] = 'C';
			break;
		case 13:
			hexresult[hexloop_cnt] = 'D';
			break;
		case 14:
			hexresult[hexloop_cnt] = 'E';
			break;
		case 15:
			hexresult[hexloop_cnt] = 'F';
			break;
		default:
			hexresult[hexloop_cnt] = static_cast<char>(hexremainder);
			break;
		}//end switch
		hexloop_cnt++;
		switch (hexval){
		case 10:
			hexresult[hexloop_cnt] = 'A';
			break;
		case 11:
			hexresult[hexloop_cnt] = 'B';
			break;
		case 12:
			hexresult[hexloop_cnt] = 'C';
			break;
		case 13:
			hexresult[hexloop_cnt] = 'D';
			break;
		case 14:
			hexresult[hexloop_cnt] = 'E';
			break;
		case 15:
			hexresult[hexloop_cnt] = 'F';
			break;
		default:
			hexresult[hexloop_cnt] = static_cast<char>(hexval);
			break;
		}//end switch
		hex_loop = false;
	} //end if

	if ((hexval >= 16)){
		switch (hexremainder){
		case 10:
			hexresult[hexloop_cnt] = 'A';
			break;
		case 11:
			hexresult[hexloop_cnt] = 'B';
			break;
		case 12:
			hexresult[hexloop_cnt] = 'C';
			break;
		case 13:
			hexresult[hexloop_cnt] = 'D';
			break;
		case 14:
			hexresult[hexloop_cnt] = 'E';
			break;
		case 15:
			hexresult[hexloop_cnt] = 'F';
			break;
		default:
			hexresult[hexloop_cnt] = static_cast<char>(hexremainder);
			break;
		}//end switch
		hexval_found = true;
	}//end if 

	hexloop_cnt++;
} //endwhile


		cout << i << "\t" << result <<"\t" << octresult<<"\t" << hexresult<< endl;
		result = 0;
		octresult = 0;
//		hexresult[5] = { ' ', ' ', ' ', ' ', ' ' };
	}



	cin.clear();
	cin.ignore(255, '/n');
	cin.get();

	return 0;
}
Last edited on
Topic archived. No new replies allowed.