CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    4

    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...

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help with maths in C++, not working properly

    That's .net, not C++. Wrong forum.

  3. #3
    Join Date
    Mar 2010
    Posts
    4

    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

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    Join Date
    Mar 2010
    Posts
    4

    Re: Help with maths in C++, not working properly

    Quote Originally Posted by Skizmo View Post
    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?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help with maths in C++, not working properly

    Quote Originally Posted by ToySoldier92 View Post
    Ah, I chose Windows Forms Application, apparently that is .net then?
    Yes.

  7. #7
    Join Date
    Mar 2010
    Posts
    4

    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..

  8. #8
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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
  •  





Click Here to Expand Forum to Full Width

Featured