# convert any base<=36 to base ten

Page 1 of 1

## 4 Replies - 18063 Views - Last Post: 28 January 2011 - 10:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=212654&amp;s=10e06c47ce2285b063b29c8391752b59&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bunch0

Reputation: 0
• Posts: 2
• Joined: 28-January 11

# convert any base<=36 to base ten

Posted 28 January 2011 - 04:14 PM

i am having trouble with the void base converter and my const char digits set. i think my order may be off or i am not using it in the code.
i need to be able to convert any number to base ten after being given the number and base.
i so far am unable to run the code. Any help would be great. thanks

```#include <iostream>
#include <cmath>
using namespace std;
void baseConverter(int number, int base);
const char digits ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
int result;
int number, base;

int main () {
cout << "This program will ask for a base of a number\n";
cout << " and the number itself and convert your number\n";
cout << " to base ten\n";
while (true) {
cout << "Please enter a number and then the numbers base: ";
cin >> number, base;

if (base < 2 || base >36) break ;
cout << "Bases must be from 2 to 36 to convert. Please try again" << endl;
}
result= baseConverter(number,base);
cout << "The converted base 10 number is: "<<result;

cin.get();
cin.get();
return 0;
}

void baseConverter(int number, int base)
{
int sum = number%10;
for (int i=base; (number/=10)!=0; i*=base)
sum+= number*i;
return sum;
}
```

Edited by Dogstopper:

Is This A Good Question/Topic? 0

## Replies To: convert any base<=36 to base ten

### #2 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: convert any base<=36 to base ten

Posted 28 January 2011 - 04:48 PM

Why have you done this?

```const char digits ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");

```

A char type can only accept a single character unless you declare an array of type char.

I'd make use of ASCII Table to give you some clues as to how to approach this problem. Create groups of characters according to this layout and save yourself a lot of extra code.

### #3 bunch0

Reputation: 0
• Posts: 2
• Joined: 28-January 11

## Re: convert any base<=36 to base ten

Posted 28 January 2011 - 05:20 PM

Not sure why i added the const char. i am still new to the C++ world.
here is what i have corrected so far.
The error C2109 - requires array or pointer type. - keeps coming up on line 40, i know about arrays but am unsure how to approach this error.

```#include <iostream>
#include <cmath>
using namespace std;
int baseConverter(int number, int base);
int result,number, base,i;

int main () {
cout << "This program will ask for a base of a number\n";
cout << " and the number itself and convert your number\n";
cout << " to base ten\n";
while (true) {
cout << "Type in a base (1 to 36): ";
cin >> base;
cout << "Type in a base " <<base << " number: ";
cin >> number;

if (base < 2 || base >36) break ;
cout << "Bases must be from 2 to 36 to convert. Please try again" << endl;

}
result= baseConverter(number,base);
cout << "The number " << number << " base " << base << "converted to base 10 is: "<<result;

cin.get();
cin.get();
return 0;
}

int baseConverter(int number, int base)
{
for(i=0;i>=0;i++)
{
result += number[i]*base^(base-i);
cout << result;

}
}
```

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 29 January 2011 - 05:00 AM

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3746
• Posts: 13,620
• Joined: 08-August 08

## Re: convert any base<=36 to base ten

Posted 28 January 2011 - 10:02 PM

Do you not see the message about code tags when you post? I find it difficult to ignore.

### #5 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: convert any base<=36 to base ten

Posted 28 January 2011 - 10:25 PM

First, what is the type of "i" in for(i=0;i>=0;i++)?

And in
```result += number[i]*base^(base-i);
```

number is an int (not an array), so number[i] is an error.