[absolute beginners]problems about base Conversion

Here are the short guide line and requirements.

Write a C++ program to convert a base 10 number to base b. The program should first prompt the user to enter two integers, one is a base 10 number, and the other is the base that she wants to convert the base 10 number to. For simplicity, we assume b should be less than or equal to 9.

C++ program should consist of a main function together with a recursive function convert that takes two parameters, (1) a base 10 value, and (2) the base. The returning value of this recursive function should be a string that keeps the converted result. To give you more ideas on what the function should look like, the function prototype is given below.
string convert(int value, int b)

A sample screen display of the program sessions is given below:
Session 1: Enter a base 10 number: 60 Enter the base you want to convert the number to: 4 The value 60 (base 10) is 330 (base 4)

and here is something what i've done.
I really don't know the connection between recursive and the fuction but recursive is a must in this case.
Can anyone sort this out for me please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <sstream>
using namespace std;

int main(){
int value;
int b;
cout<< "Enter a base 10 number:";
cin>>value;
cout<<value;

cout<< "Enter the base you want to convert the number to:";
cin>>b;
cout<< b;

string convert(value, b);
return 0; 
}




string convert(int value, int b)
{
	if(b>9 && b<1)
		return 0;
	else
		return convert(value/b,b);



Last edited on
On line 26: don't return 0, instead: ""

What you need is the modulus operator %. This provides the required digit for that base. Add '0' to get the char (for the string). Add the char to the string returned from the call of convert(). Do not call convert() recursively when (value/b) is 0.
Topic archived. No new replies allowed.