| | |
CAn any1 tell me watz wrong in da code
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 12
Reputation:
Solved Threads: 0
Hi al...I am a total c++ beginner...I need ur help plz...I am making a program which asks the user to input a binary number and displayz the binary number converted to decimal number using a function.
The code which i wrote is below:
#include<iostream.h>
#include<math.h>
int bin2dec(int n)
{
int k=0, sum=0,x;
while(n>0)
{
x=n%10;
sum+=x*pow(2,k++);
n/=10;
}
return sum;
}
void main()
{
int n,dec;
cout<<"Enter any binary number"<<endl;
cin>>n;
cout<<"The binary number converted to decimalform= "<<dec=bin2dec(n);
cout<<endl;
}
The code which i wrote is below:
#include<iostream.h>
#include<math.h>
int bin2dec(int n)
{
int k=0, sum=0,x;
while(n>0)
{
x=n%10;
sum+=x*pow(2,k++);
n/=10;
}
return sum;
}
void main()
{
int n,dec;
cout<<"Enter any binary number"<<endl;
cin>>n;
cout<<"The binary number converted to decimalform= "<<dec=bin2dec(n);
cout<<endl;
}
Last edited by Extreme; Dec 16th, 2004 at 5:19 pm. Reason: Small mistake
>>>Extreme<<<
Can you do us all a favor? Plz don't uz AOLspk in hr. U dont need 2 uz cute wurdz here.
If you're asking for help in a forum, the absolute best thing you can do is be as clear as possible, which means stop speaking like you're in some lol d00d chat room. Doing anything else will detract from your potential to recieve help.
If you're asking for help in a forum, the absolute best thing you can do is be as clear as possible, which means stop speaking like you're in some lol d00d chat room. Doing anything else will detract from your potential to recieve help.
Alex Cavnar, aka alc6379
•
•
Join Date: Dec 2003
Posts: 55
Reputation:
Solved Threads: 6
just a quick 'n' dirty hack here 

C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <math.h> using namespace std; int Bin2dec(string strBin) { int nSum = 0, k = strBin.length() - 1; for (int i = 0; i < strBin.length(); i++) nSum += (strBin[i] - '0') * pow(2, k--); return nSum; } int main(void) { string strBin; cout << "Enter binary number :"; cin >> strBin; cout << "Decimal value: " << Bin2dec(strBin) << endl; return 0; }
Theres another thread going with the same problem. I have posted C++ code and someone has also posted a C version of a binary to decimal program. The post is "I need binary to decimal code" or sumthin like that on the c/c++ main board.
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
•
•
That's not important, he wanted to have his thinggy corrected, and that's what i did...this answer was specifically addressed to his problem
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
•
•
•
•
Originally Posted by 1o0oBhP
not having a go, dont worry! just saying that if there is anything that cant be sorted then a few people have discussed the topic of going both ways (b2d, d2b) on a different thread. btw where is the pow function defined ? is it in <cmath>? would certainly simplify my own code version...
May 'the Google' be with you!
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: HELP! Illegal Character Constant in a Console Application
- Next Thread: garbage value ?
Views: 2518 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






