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: