프로그램 목적은 십진수를 16 진수로 변환하는 것입니다.1) char 배열 요소에 정수 값을 할당하는 데 문제가 있습니다. 내 배열 값에 대한 기호를 가져 오는 중이 야. 2) 배열을 공백으로 어떻게 초기화합니까? 배열 요소에서 가비지 및 오래된 데이터를 제거합니다. 3) 누군가 내 문제가 무엇인지 설명 할 수 있습니까? 정수와 문자로? 4) 귀찮은 문제가 있습니까?char 배열 요소에 정수 값 할당
#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;
}
관련 부분에 코드를 좁힐, 또는 더 나은 아직 만드십시오 [최소, 완료를하고 검증 예] (http://stackoverflow.com/help/ mcve) 우리를 보여주십시오. –
또한 들여 쓰기를하십시오. +1 요아킴! – gsamaras