hi every one,
im working on a program to convert the binary number system to the decimal one. I have the code yet im wondering if it a while loop can be used instead of a for loop. i have major problems trying to work out for loops, stupid me!

#include <iostream>
#include <cmath>
using namespace std;
void bintodec(int binarynumber, int& decimal, int& weight);
int main ()
{
int decimalnum;
int binarynum;
int bitweight;
int digit;
int weight;
decimalnum = 0; 
bitweight = 0;
cout<<"Please enter the number of digits you will be using: ";
cin>>digit;
cout<<"Enter number in binary: ";
cin>>binarynum;

bintodec(binarynum, decimalnum, bitweight);
cout<<"Binary "<<binarynum<<" = " << decimalnum << " decimal"<<endl;
return 0;
}
void bintodec(int binarynumber, int& decimal, int& weight)
{
int bit;
int digit;
for(int weight; weight <= digit; weight++)
{
bit = binarynumber % 10;
decimal = decimal + bit * static_cast<int>(pow(2, weight));
binarynumber = binarynumber /10;

bintodec (binarynumber, decimal, weight);
}
}

Here is the code, thanks so much for any1 who helps me.

Cheers
Becki:lol:

The binary-to-decimal link which Dave has given you is the 'C' way of doing things, which is fine, and you should learn to do it that way, although, C++ gives you a really nice easy way to do this - Using the <bitset> library. (a bitset is a container which only holds 1s and 0s)

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

int main()
{
    const int i = 100;
    const int total_bits = sizeof(i) * 8; // The number of bits in 'i'
    bitset<total_bits> b(i);
    cout << b << endl;
}

and, slightly more complicated, but really just the same logic in reverse

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

int main()
{
    unsigned long i;
    const int total_bits = sizeof(i) * 8; // The number of bits in 'i'
    const string s = "1100100";

    bitset<total_bits> b(s);
    i = b.to_ulong();  // turns 'b' into an unsigned long int
    cout << i << endl;
}
commented: Good-[Grunt] +1

You should use indention for proper readability, make's it easier to code and it make's it easier to help.

I'm not an very good coder, but I think You could do it with:

int weight = 0;
while(weight <= digit)
{
    //Loop code
    weight++
}

But I don't really see why you would specificly want to use an while loop, it's not good to point yourself to the type your gonna use (unless it's for learning purpose :P)

To you do

int decimalnum;
decimalnum = 0;

Which could be easily replaced with

int decimalnum = 0;

Hope it helped.

Dear becki,

Try this program with for loop only, or u can also use while loop.

But Mind well when ever you are working with any loop, check three of the thngs, initiallization, condition check and termination.

dear becki, u have missed the initiallization of one of your variable,

check your self and run the program.

Good luck.

Hey Becki,

Im not that good a coder and this program is unweildy and long but you could implement it like this:

#include <iostream>
using namespace std;
int decimal=0;
int input[10];
int i =0;
int j;
int k=0;
int a=0;
int power = 1;
 
 
 
 
 
int convert(void)
{
 
i -= 1;
 
while(a<=i)
{
 
decimal = decimal + (input[a]*power);
 
a++;
 
power = power*2;
}
 
return decimal;
 
}
 
int main(void)
 
{
 
cout<<"Input binary value, pressing enter after every value, type '2' to finish"<<endl;
 
while(j != 2)
{
if(k>0){input[i] = j;
i++; }
cin>>j;
 
k =1;
}
 
 
 
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<convert()<<endl;
 
system("pause");
 
 
 
return 0;
 
}

Sorry about the double post, but the real conversion is this bit:

int convert(void)
{

i -= 1;

    while(a<=i)
    {

        decimal = decimal + (input[a]*power);

        a++;

            power = power*2;

}

    return decimal;

}

Hey Becki,

Im not that good a coder and this program is unweildy and long but you could implement it like this:

#include <iostream>
using namespace std;

// not a good programming practice to declare global variables.
// this beats the intended use of the best object oriented language 
// around (C++). Following the principle of information hiding is best
// implemented when each logical unit has its own memory and hides
// its own implementation from the other units.

// Your program does not comply with this.

int decimal=0;
int input[10];
int i =0;
int j;
int k=0;
int a=0;
int power = 1;

int convert(void)
{

i -= 1;

    while(a<=i)
    {
        decimal = decimal + (input[a]*power);
a++;
        power = power*2;
    }
    
    return decimal;

}

int main(void)
{

     cout<<"Input binary value, pressing enter after every value, type '2' to finish"<<endl;
     
     while(j != 2)
     {
         if(k>0){input[i] = j;
                 i++;   }
      cin>>j;
      
      k =1;
     }

cout<<convert()<<endl;
     
// never use os dependent function calls and btw this call has a very 
// high overhead. Better use cin.get () in case of C++.

      system("pause"); 
return 0;

}

Good programming practices and better understanding of the problem domain is the key to becoming the best programmer around.

Hope it helped, bye.

Good programming practices and better understanding of the problem domain is the key to becoming the best programmer around.

Hope it helped, bye.

Hey this problem is too simple to implemented in an objecft oriented way. Programs this small do not need to have good programming practices. Especially if teyt are just for fun. For somthing this small it only matters if it works IMO. Yes is you are coding some massive trading system for a bank then ofc i should do that.

Hey this problem is too simple to implemented in an objecft oriented way. Programs this small do not need to have good programming practices. Especially if teyt are just for fun. For somthing this small it only matters if it works IMO. Yes is you are coding some massive trading system for a bank then ofc i should do that.

Looks like you have never undertaken a large project at hand otherwise you wouldnt be saying such a thing. Learning things the hard way should not be at the top of your list :)

In the end, just remember one thing "Old habits die hard".

Hey this problem is too simple to implemented in an objecft oriented way. Programs this small do not need to have good programming practices. Especially if teyt are just for fun. For somthing this small it only matters if it works IMO.

Well said, if all you want to be is a hobby programmer. For anyone looking at programming as a profession, this is the worst advice they can receive.

All programs should be written using good programming practices. It never "only matters if it works." Have pride in your work.

Im not that good a coder and this program is unweildy and long...

Now we know why... ;)

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.