hey alll..I posted earlier on needing help for a code on input #..base..print decimal form..I actually didnt learn this in high school so thats why I was having so much trouble :@

Now that I read up and understand the logic in bases..I was thinking that for my code I would use an algorithm that would convert between bases..

I was thinking:
divide input by 2
use remainder
so lets say for 6
n n/2 remainder
6 3 0
3 1 1
1 0 1

then just convert from binary to the input..which is where I am lost..converting from binary to any base

thanks all!

What if the base is binary and the number is 1?

I am guessing there is some type of algorithm that works for bases..ive been thinking and cant get.. Any recommendations
Tyyy

anyone plz.. :((((((

>> hey alll..I posted earlier on needing help for a code on input #..base..print decimal form.


You have to link it.


What's the problem statement? Usually there is some example input and example output.


>> "input #..base..print decimal"


I'm guessing this means the user types in a number and the base that the number is in and you convert it to decimal.

I was thinking:
divide input by 2
use remainder
so lets say for 6
n n/2 remainder
6 3 0
3 1 1
1 0 1

No clue what you're trying to do here. I guess the number is 6, the base is 3 (or is it 2), and you're converting 6 in decimal to "101", which isn't 6 in base 2 or base 3, and my interpretation of "input #..base..print decimal" was that you read in the number in a base other than 10 and convert it to base 10 and you seem to be doing the opposite here.


All in all, more clarification of the assignment and where you are stuck is needed.

assignment is to have a number saved in a string, then convert it from a string to an integer [have that done] and finally convert that number to a decimal number from whatever base the user inputs.

string snum;
	int n=0, base;

	cout << "Enter n: ";
	cin >> snum;
	
	for (int i=0; i<snum.length(); i++) //descending order
	{	int digit = snum.at(i) - '0';
		n = n*10 + digit;
	}

	cout << "n is " << n << endl;

1. assignment is to have a number saved in a string

I'm with you so far.


2. then convert it from a string to an integer

Still with you.

3. [have that done]

Good. Still with you. I assume that is lines 2 and 7 to 10.


4. and finally convert that number to a decimal number

You lost me here. I thought you had already done that in lines 2 and 7 to 10. In your conversion from string to int, you already assume that the user entered the number in base 10 since in line 8 you apparently are accepting '0' through '9' as valid digits and you are multiplying by 10 in line 9. Hence you are already assuming decimal / base 10 as input. The conversion has already been made to decimal in steps 2 and 3 above.


5. from whatever base the user inputs.

You definitely lost me here. I see a variable called "base", but it's unused. You never ask the user for input for the base.


So you are converting either FROM some user-specified base to base 10 or you start with base 10 and convert TO a different base. I'm not clear which one this is. From your wording it seems like you are converting TO base 10...

convert that number to a decimal number from whatever base the user inputs.

Assuming this sentence is an accurate sentence, you need to rewrite lines 7 to 10. The input will not be in base 10 so for sure line 9 is is wrong since you're multiplying by 10. Find out if the user can specify a base ABOVE base 10. If so, you'll need to be able to handle digits like 'A', 'B', etc. You may also want to implement some input validation on line 8 if that's required. What if I am entering a number if base 6 and I enter '8' as a digit? Find out if you are supposed to catch and reject that.

Anyway, at the very least, you need to ask the user to input the base and store it in the base variable. You'll need to use that base variable in the conversion. So again, more clarification of the actual assignment is needed. I'm assuming the input and output is supposed to look something like this...

Enter the base : 7
Enter a number in base 7 : 245


245 in base 7 is 131 in decimal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.