1. ## need help here..

hi i just finished my program but it won't do the input as required by me..
some digits are missing and i can't find it out
basically it is a program that ask user's for input integer and base number
here's the coding

Code:
```#include <iostream>
#include <cmath>

using namespace std;

void convert(int , int, int, int*); //convert(number, base, length, result)

int len=0, i;

int main()
{
int num, base, *r;

cout<<"\nEnter a number: ";
cin>>num;
cout<<"\nEnter base: ";
cin>>base;

len=(int)log10(num)/log10(base)+1; //Number of digits of the converted number

r=new int [len];

convert(num, base, len, r);

cout<<endl;

for(i=0; i<len; i++)
cout<<*(r+i);

delete [] r;

return 0;
}

void convert(int n, int b, int len, int* res)
{
for(i=0; i<len; i++)
{
*(res+len-i-1)=n%b;
n/=b;
}
}```
like this input i put on
Enter an integer: 89
89 in Base 2 : 1011001>>1001

89 in Base 3 : 10022>>>>022

89 in Base 4 : 1121>>>>>21

89 in Base 5 : 324>>>>>>24
how to solve this problem???
any ideas??

2. Originally Posted by MyRedz
here's the coding
Good to see that you posted code in code tags, but please indent your code properly.

Originally Posted by MyRedz
how to solve this problem???
any ideas??
Use a debugger. In particular, examine the value of len. You will find that it is smaller than expected because of a mistake in casting.

Also, both len and i should be local variables. In fact, the scope of i should be limited to the for loops.

EDIT:
Consider:
• Using std::string or std::vector instead of manual memory management
• What happens when the base is greater than 10?
• Turning on compiler warnings to a higher level (the casting problem could have been detected with a warning)
• Using static_cast instead of C-style casts (it would also have helped prevent the problem)
• Using array notation instead of pointer notation when you use array indices (e.g., r[i] instead of *(r+i))

3. Originally Posted by MyRedz
Code:
`len=(int)log10(num)/log10(base)+1; //Number of digits of the converted number`
this won't work!
should be:

Code:
`len=(int)(log10(num)/log10(base)+1); //Number of digits of the converted number`
calculate first, later convert to int.

I tried in VC++, its output is just good.

4. auralius, that is what I meant by a mistake in casting. Apparently you were less confident than I was of MyRedz's debugging skills

5. I'm speechless...

6. question here..
how do i toget it put text before the answer like the answer is..
i tried
for(i=0; i<len; i++)
but the output is meessed up then..
how to make it into separate functions i mean for.
base 2 base 4 base 6.
this coding only allow more to do for one base and input at a time ..

7. You do it right, but don't forget to put a space! They aren't automatically put in there, you know...

8. Originally Posted by MyRedz
i tried
Code:
```for(i=0; i<len; i++)
but the output is meessed up then..
can't be like that...
string "the answer is" will be reprinted several times...

should be:

Code:
```cout << "the answer is  " ;
for(i=0; i<len; i++)
cout << *(r+i);```

9. ok looks good here well.
then my question ask for a function prototype to receive two arguments only that is the user inputed base and integer value.
how to do this.
must the value and calculation of len and rbe defined as global variable or compute it separately in each function
well here's my coding but one error..
am i right here
Code:
```#include <iostream>
#include <cmath>

using namespace std;

void convert(int , int, int, int*); //convert(number, base, length, result)

int len=0, i,*r;

int main()
{
int num, base ;

cout<<"\nEnter a number: ";
cin>>num;
cout<<"\nEnter base: ";
cin>>base;
convert(num, base );
len=(int)(log10(num)/log10(base)+1); //Number of digits of the converted number
r=new int [len];

cout<<endl;

for(i=0; i<len; i++)
cout<<*(r+i);

delete [] r;

return 0;
}

void convert(int n, int b)
{

for(i=0; i<len; i++)
{
*(r+len-i-1)=n%b;
n/=b;
}
}```

10. Originally Posted by MyRedz
then my question ask for a function prototype to receive two arguments only that is the user inputed base and integer value.
What is the rationale for such restrictions on the number of parameters?

Originally Posted by MyRedz
must the value and calculation of len and rbe defined as global variable or compute it separately in each function
No, do not use global variables. #include <vector> and use a std::vector<int>, e.g.,
Code:
```std::vector<int> convert(int number, int base);

// ...

cout << "\nEnter a number: ";
cin >> num;
cout << "\nEnter base: ";
cin >> base;
cout << endl;

std::vector<int> result = convert(num, base);
for (std::vector<int>::size_type i = 0, len = result.size(); i < len; ++i)
cout << result[i];```
Note that you no longer need to use delete[] since std::vector performs the memory management for you.

If for some reason you cannot use std::vector, then the next simplest alternative is to perform the dynamic memory allocation in the convert function and then return a pointer. You would then compute the length twice: once in the convert function and once in the main function.

If you happen to be taking a class with a teacher who insists that convert must take two arguments of type int and have a void return type, then go ahead and use global variables, but know also that your teacher would be teaching you poor programming practices.