I need help with an assigment - I am to create a number systems table which consist of converting a decimal number to binary, octal, and hexadecimal. Here is my code i have so far but does not create output - any suggestions would be great - thanks

#include <iostream>
#include <string>
using namespace std;

int main()
{
int num = 0;        //loop count for binary numbers
int fact = 256;     //factor for binary values          
double dec = 0.00;
int bin;
int oct;
int hex;

cout << "Decimal\t\t" << "Binary\t\t" << "Octal\t\t" << "Hexadecimal" << endl;
dec = k;
cout << k;

for (k = 1; k <= 256; k++)
num = k;
        if (num = 256)
            cout << '1';
        else
            cout << '0';

    do{
        if (num < fact && num >= fact / 2)
            cout << '1' << endl;
        else
            cout << '0' << endl;
    fact = fact / 2;
    num = num % fact;
    while (fact > 1);
    }
cout << "\t\t\t\t" << k;
cout << "\t\t\t\t\t\t" << k;
return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Couple of things.
1) k is not defined anywhere.
2) The do/while construct looks like do { } while (condition); So you may want to make the following change:

num = num % fact;
while (fact > 1);
}
cout << "\t\t\t\t" << k;

to

num = num % fact;
} while (fact > 1);
cout << "\t\t\t\t" << k;

You can't represent an integral number in binary form but you can use a string though it would not serve you for anything.There is an algorithm here http://www.wikihow.com/Convert-from-Decimal-to-Binary for you which you might want to take a look at.And here's an example too.

#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>

std::string DecToBin (int);
int DecToHex (int);

int main ()  {
   std::cout << DecToBin (44531) << "\n";
   std::cout << DecToHex (352);
 }

int DecToHex (int Nb)  {
   std::ostringstream o;  //you can use streams for formating 
   int x;
   o << std::hex << Nb;  
   std::istringstream i(o.str());
   i >> x;
   return x;
 }

std::string DecToBin (int Nb)  {
   int exp=2;  
   std::string Bin;
   while (exp<=Nb)  {
      exp*=2;   
    }
   if (exp>Nb) exp-=exp/2;

   while (exp>0)  {
      if (Nb-exp > 0)  {
         Nb-=exp;
         exp-=exp/2;
         Bin+="1";
       }
      else if (exp==1)  {
        if (Nb-exp >=0)  {
           Bin+="1";
           break;
         }
        else {
           Bin+="0";
           break;
         }
       }

      else  {
         Bin+="0";
         exp-=exp/2;
       }
    }
   return Bin;   
 }
This article has been dead for over six months. Start a new discussion instead.