4 Replies - 5782 Views - Last Post: 28 January 2011 - 10:25 PM Rate Topic: -----

#1 bunch0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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: :code:

Is This A Good Question/Topic? 0
  • +

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

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 bunch0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!!!

:code:

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

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2985
  • Posts: 10,321
  • 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.
Was This Post Helpful? 2
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: convert any base<=36 to base ten

Posted 28 January 2011 - 10:25 PM

:code:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1