2017-10-22 1 views
0

** # 1 정말로 도움을 주셔서 감사합니다. :) **C++ 내가 예상 한대로 다음 코드가 작동하지 않는 이유

// Decimal_to_Binary

#include<iostream> 
#include<Windows.h> 

using namespace std; 

int main() { 

    //Variables: 
    int num_b2[8]; 
    unsigned int delay_x = 0, num_b10 = 0, x = 0; 

    //Input: 
    cout << "\n Enter a decimal(integer) number (0~31) \n To get its equivalent binary number : "; cin >> num_b10; cout << "\a"; 
    x = num_b10; 
    system("cls"); 

    //Function: 
    while (x > 0) { 
     for (unsigned short int i = 7; i > 0; i -= 1) { 
      num_b2[i] = x % 2; 
      x /= 2; 
     } 
    } 

    //Output: 
    Sleep(delay_x); 
    for (unsigned short int i = 0; i<sizeof(num_b2)/4; i += 1) { 
     cout << num_b2[i]; 
    } 
    cout << "\n\n\n"; 


    system("pause"); 
    return 0; 
} 

** # 1 난 정말 당신이 제공 할 수있는 모든 도움을 주셔서 감사합니다. :) **

+1

에 진수에 대한 간단한 코드입니다. – Yunnosch

+0

Eric Lippert의 [작은 프로그램 디버깅 방법] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 읽고 디버거를 사용하여 코드를 단계별로 한 줄씩 살펴보십시오. 또한 단일 행 - 여러 줄로 된 다중 명령문을 사용하여 디버깅을 쉽게 수행 할 수 있습니다. –

+0

(이상한 코드가 있음에도 불구하고) 작동하는 것으로 보입니다. 당신은 다른 무엇을 기대합니까? –

답변

0

여기에 귀하의 질문에 형식 코드를 배울 바랍니다 바이너리 변환

<code>int num = 5; 
    for (int i = 31; i>=0 ; i--) 
    { 
     if((1 << i) & num) 
      cout << 1; 
     else 
      cout << 0; 
    }</code> 
관련 문제