# Thread: What Am I Doing Wrong?

1. ## 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. Please state the problem you have been toying with.

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

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

7. What Am I Doing Wrong?
Posting C++ question on C-forum?