i am doing a decimal to binary conversion and i have figure out how to do the conversion i just don't know how to make it cout so that it is in reverse.... i was also told that it could done in a while loop

here is the code

1. #include<iostream>
2. using namespace std;
3. int main(){
4.
5. int dec,olddec,remain;
6.
7. cout<<"please input a decimal to be converted to binary:"<<endl;
8.	cin>>dec;
9.	cout<<endl;
10.	
11.	while(dec>0){
12.
13.	olddec=dec;
14.
15.	remain = dec % 2;
16.	
17.	dec /=2;
18.	
19.	cout<<olddec<<"/2 = "<<dec<<"  "<<"remainder = "<<remain<<endl;
20.	}
21.
22.	return 0;
23. }

i am doing a decimal to binary conversion and i have figure out how to do the conversion i just don't know how to make it cout so that it is in reverse.... i was also told that it could done in a while loop

here is the code

1. #include<iostream>
2. using namespace std;
3. int main(){
4.
5. int dec,olddec,remain;
6.
7. cout<<"please input a decimal to be converted to binary:"<<endl;
8.	cin>>dec;
9.	cout<<endl;
10.	
11.	while(dec>0){
12.
13.	olddec=dec;
14.
15.	remain = dec % 2;
16.	
17.	dec /=2;
18.	
19.	cout<<olddec<<"/2 = "<<dec<<"  "<<"remainder = "<<remain<<endl;
20.	}
21.
22.	return 0;
23. }

Well it looks to me like you are calculating the digits in the reverse order that you want to display them. remainder is the digit. You can't just print remainder in the order that you calculate it because then the digits are in reverse order, and currently remainder is writing over itself. Currently you HAVE to write over remainder before displaying because you have no place to store the old remainder.

Solution: use an array that sets aside an integer for every digit. Since you are using integers and integers have 32 bits, you might as well make an array called digit that would hold 32 integers. So say the user typed in the number 18. In binary, that is:

10010

Working right to left, by the end of your calculation, you'd want your digit values to be this for the above number:

digit[0] = 0
digit[1] = 1
digit[2] = 0
digit[3] = 0
digit[4] = 1

You'd display the results in this order: digit[4], then digit[3], then digit[2], then digit[1], then digit[0]. The result is this:
10010

Make sure you initialize all of the digits to 0 first or have some way of keeping track of the number of digits in the number.

keep on dividing number till it gives true value.

while( n%10 => 0)

{
conversion code
}

I believe a common way to get around the reversal is to use a string to store the output:

void binToString(int x){
   string binStr;
   
   while(x > 0){
      if(x & 1)      // same as if(x % 2)
         binStr = "1" + binStr;
      else
         binStr = "0" + binStr;
      
      x >>= 1;       // same as x = x / 2
      }
   
   cout << binStr << endl;
   }

ok i am really bad at strings since my professor barely explained how to use them so i am trying to avoid for this project i have to do till i can try it with simpler programs;
i tried an array but it does not seem to want to output the numbers when it run it gives a - number that are not in 0's and ones

#include<iostream>
using namespace std;
//int dec(int []); // for after i get program to run i want to turn it into function

int main(){

int dec,olddec,remain,i,p[50];
cout<<"please input a decimal to be converted to binary:"<<endl;
	cin>>dec;
	cout<<endl;
	
	while(dec > 0){
	i = 0;

	olddec = dec;

	remain = dec % 2;

	p[i]=remain;

i++;

	dec /=2;
	
	cout<<olddec<<"/2 = "<<dec<<"  "<<"remainder = "<<remain<<endl;
	}
	for (;i>=0;i--){
	cout<<p[i];
	}
}

ok i am really bad at strings since my professor barely explained how to use them so i am trying to avoid for this project i have to do till i can try it with simpler programs;
i tried an array but it does not seem to want to output the numbers when it run it gives a - number that are not in 0's and ones

#include<iostream>
using namespace std;
//int dec(int []); // for after i get program to run i want to turn it into function

int main(){

int dec,olddec,remain,i,p[50];
cout<<"please input a decimal to be converted to binary:"<<endl;
	cin>>dec;
	cout<<endl;
	
	while(dec > 0){
	i = 0;

	olddec = dec;

	remain = dec % 2;

	p[i]=remain;

i++;

	dec /=2;
	
	cout<<olddec<<"/2 = "<<dec<<"  "<<"remainder = "<<remain<<endl;
	}
	for (;i>=0;i--){
	cout<<p[i];
	}
}

Two problems, both involving i. Problem 1, you are initializing i to 0 every trip through the loop in line 13. i needs to be initialized once, and before the loop. You are thus rewriting over old calculations. Two, i is too large when you get to your for-loop in line 27. If a number has 5 digits, you are going through that loop 6 times. You want to go through it 5 times, once per digit. The nonsense digits that you see at the beginning of the number is that extra integer that you do not want to display, but that you DO display in your program. You did not initialize your array to 0, so that memory location has not been touched by your program. If the number 45 was in there before the program started, that's what will be outputted, in addition to the REAL digits.

Eureka...!!!!!!!!Thank you all for the help and the little bits of knowledge that i have gained due to your efforts......

this is the finished product... seems i was wrong if the input is 0 the number becomes a large - number

#include <iostream>
using namespace std;
int main(){

int dec,olddec,remain,i,p[50];
cout<<"please input a decimal to be converted to binary:"<<endl;
	cin>>dec;
	cout<<endl;
	
	i=0;

	while(dec > 0){

	olddec = dec;

	remain = dec % 2;

	p[0+i]=remain;

i++;

	dec /=2;
	
	cout<<olddec<<"/2 = "<<dec<<"  "<<"remainder = "<<remain<<endl;
	}
	for (;i-1>=0;i--){
	cout<<p[i-1];
	}
	cout<<endl;
	return 0;
}

Eureka...!!!!!!!!Thank you all for the help and the little bits of knowledge that i have gained due to your efforts......

this is the finished product... seems i was wrong if the input is 0 the number becomes a large - number

#include <iostream>
using namespace std;
int main(){

        int dec,olddec,remain,i,p[50];
        cout<<"please input a decimal to be converted to binary:"<<endl;
	cin>>dec;
	cout<<endl;
	
	i=0;

        if (dec == 0)   // trivial case.  Just display 0.
             cout << 0;
        else        // start of else
        {

	while(dec > 0){      // start of while

	olddec = dec;

	remain = dec % 2;

	p[0+i]=remain;

        i++;

	dec /=2;
	
	cout<<olddec<<"/2 = "<<dec<<"  "<<"remainder = "<<remain<<endl;
	}        // end of while
	for (;i-1>=0;i--){
	cout<<p[i-1];
	}
        }       // end of "else"
	cout<<endl;
	return 0;
}

Not a huge problem. If the number is 0, there's nothing to calculate, so I would just short-circuit the problem with an if-else statement. If the number entered equals 0, just display 0. If it's larger than 0, go through your while loop. I added an if-else loop above. The bulk of your original code is in the "else" part.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.