Thread: Help with C++ Math

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Smile Help with C++ Math

    Hello, I am new to these great forums here and new to C++ too

    Anyway, I am coding a binary to decimal converter and I have the math right and everything but C++ does not want to play nice with the math.

    here is the output:
    Code:
      Enter a binary number: 11110000
    
    1 = 98
            1 =  (2 ^ 0) * 1
    1 = 147
            1 =  (2 ^ 1) * 1
    1 = 0
            1 =  (2 ^ 2) * 1
    1 = 49
            1 =  (2 ^ 3) * 1
    0 = 288
            0 =  (2 ^ 4) * 0
    0 = 336
            0 =  (2 ^ 5) * 0
    0 = 192
            0 =  (2 ^ 6) * 0
    0 = 240
            0 =  (2 ^ 7) * 0
    the decimal number shows the output C++ gave for the digit, the tabbed line under it shows the math it did to get that number, if you do the math manually the output should be different.

    Here is my code:
    Code:
    char inText[32];
    int final;
    
    cout << "\n  Enter a binary number: ";
    cin >> inText;
    cout << "\n  Decimal Result: \n";
    
    for(int length = 0; length < strlen(inText); length++)
    {
            // The forumla
            final += 2 ^ length * inText[length];
    
            // Shows 1 = $output
            cout << inText[length] << " = " << ((2 ^ length) * inText[length]) << "\n\t";
            // Shows 1 = (2 ^ $len) * $digit
            cout << inText[length] << " = " << " (2 ^ " << length << ") * " << inText[length] << "\n";
    }
    
    cout << final;
    cout << "\n\n";
    Thanks in advance
    Last edited by aonic; 01-29-2005 at 03:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  3. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  4. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM
  5. For the math junkies
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-14-2003, 05:09 PM