CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 9 of 9

Thread: My program works good but there are two things I don't understand yet

Threaded View

  1. #1
    Join Date
    Mar 2005

    My program works good but there are two things I don't understand yet

    I'm working with a program that converts an integer to it's binary equivalent. My program works fine. Now to get into some details: If I enter the integer 148 my program will output 10010100 which is what I want. I want it to output all eight bit places. But if I enter the integer 58 it only outputs this: 111010. It omits the leading two bits which are 00. So I want it always to output all eight bit places (00111010). I don't know what to add.

    And also the last loop in the function is written so variable j gets set to whatever i is minus one. I don't get the why of that.

    Here is what I got:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    void convertToBinary(int n);
    int main()
    	int x;
    	cout << "Enter an integer: ";
    	cin >> x;
    	return 0;
    void convertToBinary(int n)
    	int binaryNumber[8];
    	int i = 0;
    	while(n > 0)
    		binaryNumber[i] = n % 2;
    		n /= 2;
    	cout << "The binary equivalent is ";
    	for(int j = i - 1; j >= 0; j--)
    		cout << binaryNumber[j] << " ";
    Last edited by 357mag; September 8th, 2019 at 03:56 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)