-
March 24th, 2010, 08:53 AM
#1
Help with maths in C++, not working properly
Code:
System::String ^ bin2dec( System::String ^ input )
{
__int32 decvalue = System::Convert::ToInt32( input );
int dec = decvalue;
System::Array::Reverse( input->ToCharArray() );
if ( input->Length > 0 )
{
int x = 0;
for each ( int row in input->ToCharArray() )
{
dec = dec + ( row * 2 ^ x );
x = x + 1;
}
}
return "Result: " + dec.ToString() + " <";
}
Now, I'm pretty much a complete noob and I have no idea why it's doing weird stuff like this
When input is: 0
Output becomes: 96
When 1: 99
When 2: 102
When 3: 105
When 4: 107 [etc]
While it should convert the binary number to a decimal one. Now, I am not sure if it's the formula which is wrong, or wether it's the code. I'm guessing on the second one, as I'm pretty sure I am doing something wrong, but I don't know what...
-
March 24th, 2010, 08:59 AM
#2
Re: Help with maths in C++, not working properly
That's .net, not C++. Wrong forum.
-
March 24th, 2010, 09:21 AM
#3
Re: Help with maths in C++, not working properly
How can this be .net when it's made in Visual C++ Express Edition? :S
/More confused
-
March 24th, 2010, 09:38 AM
#4
Re: Help with maths in C++, not working properly
How can this be .net when it's made in Visual C++ Express Edition?
Visual Studio is capable of many project types. You have to choose which type you want when you create a new project.
-
March 24th, 2010, 09:45 AM
#5
Re: Help with maths in C++, not working properly
Originally Posted by Skizmo
Visual Studio is capable of many project types. You have to choose which type you want when you create a new project.
Ah, I chose Windows Forms Application, apparently that is .net then?
-
March 24th, 2010, 09:53 AM
#6
Re: Help with maths in C++, not working properly
Originally Posted by ToySoldier92
Ah, I chose Windows Forms Application, apparently that is .net then?
Yes.
-
March 25th, 2010, 03:41 AM
#7
Re: Help with maths in C++, not working properly
>< That explains why the things I was trying aren't working. Would you mind helping me out with this problem? Still would like to get it working..
-
March 25th, 2010, 03:52 AM
#8
Re: Help with maths in C++, not working properly
Well, you are doing some things wrong.
Code:
int dec = decvalue;
This gives you the answer you want. You can stop at this stage.
Code:
dec = dec + ( row * 2 ^ x );
row is the ascii code, so 0 will b 48, 1 will be 49 etc. For single digit in put, you are adding 2 * row to your original correct answer. So, for input of e.g. 1, you get 1 + 2*49 = 99.
Also, I think you should get 108, not 107 for input of 4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|