What Am I Doing Wrong?

This is a discussion on What Am I Doing Wrong? within the C Programming forums, part of the General Programming Boards category; I have been toying with this problem for a bit and it is starting to annoy me. I cannot find ...

  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
    Katy, Texas
    Posts
    2,309
    Please state the problem you have been toying with.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    What Am I Doing Wrong?
    Posting C++ question on C-forum?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 09: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, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21