Question:
can't find whats wrong with my program c++?
?
2014-10-16 13:33:58 UTC
so this is my program
if i insert 150 after i run im expecting to get this
1 0 0 1
but im getting 1 0 0 0
why ?

#include "stdafx.h"
#include "iostream"
using namespace std;
int main()
{
int a=128 ,b=64 ,c=32 ,d=16 ,e=8 ,f=4 ,g=2 ,h=1 ,x ,m ,l ,i ,k ,j;
cout << "type a number to convert from decimal to binary" ;
cin >> x;

if (x>=a)
{
cout <<" 1 " ;
l=x-a ;
}
else
{
cout << " 0" ;
x=l;
}

if (l>=b)
{
cout<<" 1" ;
i=l-b ;
}
else
{
cout << " 0" ;
l=i ;
}
if (i>=c)
{
cout<<" 1" ;
j=i-c ;
}
else
{
cout << " 0" ;
i=j ;
}
if (j>=d)
{
cout <<" 1" ;
k=j-d ;
}
else
{
cout << " 0" ;
j=k;
}

cout << " " << k << endl;
cin >> m ;
return 0;
system("pause");
}
Three answers:
_Object
2014-10-16 16:09:55 UTC
Perhaps try something like this:



Don't forget to add whatever includes you need (maybe the PCH, stdafx.h). Next time, instruct MSVC to create an "empty project" and don't include that file.



# include

# include



int main (int, char **) {

    uint64_t N = 150;



    const uint64_t Radix = 2; /* Convert N to Radix (base 2, or change it) */



    for (int Place = log (N) / log (Radix); Place >= 0; Place --)

        std:: cout << static_cast (N / pow (Radix, Place)) % Radix;

}



Your program is wrong because you are trying to do bitmath in decimal while using useless names and invoking a significant amount of undefined behavior.



I get what you're trying to do, but it's basically this:

/* First, realize 150 = 0b1001 0110. */

uint8_t N = 150;

for (int8_t Place = 7; Place >= 0; Place --) {

    bool Bigger = false;

    if (Bigger = (N >= (0x1 << Place))) { /* If N > 2^Place */

        N -= (0x1 << Place);

    }

    std:: cout << Bigger;

}



You should surely use a loop for that.



A better way would be to simply test the actual bits of the number itself:

uint8_t N = 150;

for (int8_t Place = 7; Place >= 0; Place --)

    std:: cout << (N & (0x1 << Place));



This works as long as you can guarantee the width (8 bits, in this case) of the value, and you only want binary.



The advantage of the first method is that it will convert to any base for any size input (up to a limit in precision). Math is cool.

Actually it's quite buggy, but that's that.
?
2014-10-16 13:47:57 UTC
First what you expect is wrong... 150 is even therefore the last digit is a 0 not a 1, secondly you are making it way too complicated. For 8 bit unsigned stuff.



You might not understand the bitwise operation until you read about it.



#include

using namespace std;

int main()

{

unsigned int x;

cout << "type a number to convert from decimal to binary: " ;

cin >> x;



for(int i=0; i< 8; i++)

{

cout << ((x&0x80) ? 1 : 0);

x<<=1;

}



cin >> x ;

return 0;

}
?
2014-10-16 14:59:21 UTC
As Jeff pointed out, it is way too complicated. And if I understand what you are trying to do, it has lots of bugs.

For example:

In the else portion of your first if statement: if (x>=a)...

you print a 0, then assign: x=l; But l has not been initialized, so it could be anything, and after this assignment x could be anything. You are lucky that your example does not execute this code branch.

Then in your second if statement: if(l>=b)... you test this variable which will be uninitialized if the first if statement was false, and decide what to do next based on an uninitialized variable.

And again in the else portion of this if statement you assign the value of the uninitialized variable i to l.

And you continue to repeat this error in each if statement.



Now the compiler probably very nicely initializes your uninitialized variables to 0. So that is why you get the results you are seeing, because after the first if statement you are always testing against 0, not the value you are trying to convert.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...