954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary to Decimal HELP :(

so i have spent 3 hours trying to figure out how exponents work in C++ but now. my if statement isnt working. even if i punch in a 0 it spits out 1
thanks in advance

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void binToDec(string getBinary);
void main()
{
	string getBinary;
	cout<<"Enter a Binary Number:"<<endl;
	cin>>getBinary;
	binToDec(getBinary);
	

}
void binToDec(string getBinary)
{
	int setLength,total=0;
	double placeholder=0;
	setLength=getBinary.size();
	
	for(int i=setLength;i>0;i--)
	{
		int num=pow(2.0,placeholder);
		int test=getBinary[i];
		if (test=1)
		{
			total=total+num;
		}

		placeholder=placeholder +1;
	cout<<total<<endl;
	}
	


}
egolovin
Newbie Poster
11 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

1) Comparison of test with 1 has two equals signs, not 1 if (test == 1) . Having one equals sign assigns test to 1.

2) main() returns int, not void. Wash your mouth out with soap for returning void.

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

Hmmm shouldn't
if (test=1)
be
if (test == 1)

Anyway I read that it isn't good to have void main().

dexter1984
Light Poster
49 posts since Jan 2008
Reputation Points: 31
Solved Threads: 2
 

haha im just making small programs for class thanks for the help. stupid mistake

egolovin
Newbie Poster
11 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

It still won't work. You are comparing char with int:
getBinary[i] is char, and 1 is int.
char != int!!!

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 
It still won't work. You are comparing char with int: getBinary[i] is char, and 1 is int. char != int!!!


It will work. Comparing char with int is allowed.

Worst case is a compiler warning (eg mismatch of precision of operands). A compiler warning means the programmer should check and make sure the code has the intended result, not that it is disallowed.

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

This thread seems to be predecessor to http://www.daniweb.com/forums/thread150474.html

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

It will work. Comparing char with int is allowed.

Worst case is a compiler warning (eg mismatch of precision of operands). A compiler warning means the programmer should check and make sure the code has the intended result, not that it is disallowed.

Yes, but does he want to compare test with '1' or with 1? Because that's different thing!

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 
Yes, but does he want to compare test with '1' or with 1? Because that's different thing!


Ah, yes. Good point. Something for the original poster to contemplate .....

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You