Thread: What Am I Doing Wrong?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    22

    What Am I Doing Wrong?

    I have been toying with this problem for a bit and it is starting to annoy me. I cannot find the fault in this program. I believe the problem is due to the use of number[index]. Please help, thanks!

    Code:
    #include<iostream>
    #include<string>
    #include<cmath>
    using namespace std;
    
    int main (void){
    
            int base, index, total=0;
            string number;
            double counter = 0;
    
            cout << "\n\nPlease input base: ";
            cin >> base;
    
            cout << "\nPlease input number: ";
            cin >> number;
    
            index = (number.length() - 1);
    
            for(index ; index >= 0 ; index--){
                    total += pow(base,counter) *  number[index];       
                    counter++;
            }
             
            cout << endl << number << " in base " << base << " is " << total
            << " in base 10.\n";
            
            return 0;
            
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Please state the problem you have been toying with.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    Well if I run that program I get a bogus answer. For example if I input 2 as the base and 111 as the number the answer should be 7. If I take out number[index] from the highlighted part it works for that number. This is unacceptable, what if my input is base 2 and a number of 101. I would still get 7 when I should get 5.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    When you multiply by number[index], you are grabbing a character '1', which is x'31' in ASCII and multiplying that by the result of the pow() function. Is that what you want to happen?
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    I thought I was taking the value '1', not ASCII '31'. Should I make number an int, then process it with a modulo instead? Thanks!

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You could subtract a '0' from the '1' to get x'01'. Or, you could .atoi() it. There are several ways to attack the problem of converting a string of any base to decimal.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What Am I Doing Wrong?
    Posting C++ question on C-forum?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM