Click here to Skip to main content
15,214,155 members

Hi...need help with Decimal to Binary Conversion

Cartomania asked:

Open original thread

Im relatively new to C++ but i want to become quite good in it
and not just do it to pass some subject.

So now everyday im improving a piece of my program to try and learn more and not unlearn what
I have already learned.

So now im making a Decimal to Binary conversion but im not using any pre made "Libraries" or "Classes" otherwise I feel like I havent really done anything.

I already have a piece of code that works out the Binary for anything INfront of a dot " 'xxx'.xxx " but
now im working on the part after the dot.

So I took out my Digital book and checked up how to do it with the multiplication by 2 move and coded it so it works on some fractions quite well.
HowEver using that ends up in an infinite loop allot of the times because every time I get something like 0.60 and work it out I somewhere along the line end up with 0.60 again thus ending in an infinite loop.

I have no idea how to let the program sense a repetition in the string that im creating.

Improved Question here.
My Question is...What would be a neat method of making the program realize hes running into and Endless Loop?
Is there some way I can make it sense that its working out something that it already worked out and that it should stop?

Here is the Code so far.

#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;
double a;

double fracvalue(double x) 
 int z;            
       z = (x/1);
       a = x-z;
        return (a);

int main(int argc, char** argv) {
    double x;
    int z, c;    
    cin >> x;
    double a =fracvalue(x)*2;
    cout << fracvalue(x) << "      " << a  << endl;//Checking if A and fracvalue are giving correct readings 
    string Bin = ".";
    for (int = i=;i < 20;i++)  //<<-- THIS was while(a != 0.0) but it never seemed to 
                      //stop even when it gets to 0.0 also it doesnt write out the complete binary this way
                      //sometimes only leaving dots or partial BIN numbers.
          if (a > 1)
                Bin = Bin +'1';
                a = a-1;
                cout << a << endl;
                a = a*2;
                cout << a << endl;}
             if (a < 1)
                    Bin = Bin + '0';
                    a = a*2;
                    cout << Bin << endl;
    return 0;

For some reason it keeps on moaning about the line
z = (x/1); in the function fracvalue,the funny thing is I didnt touch this code :s it just didnt want to run anymore.

Anyway...It seems to give the right Binary if i insert large fractions but simple ones would come out only 0s or 1s and not both.
Larger ones however "Under while (a != 0.0)" will never end because it math itself loops infinitely.

Tags: C++


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100