Hi, the program is asking the user for a number ant he base of the
number, then it will convert the number to decimal.
Hi,
just some quick "didactic" remarks:
But is running right only with base 2, 3, 4, 5, 6, 7, 8 but if I try
16 it is not working.
At a glance the case base = 9 shouldn't be any different, though I
haven't run the program.
[...]
Strictly speaking you're better off also #including <ostream> and
AFAIK, you're not using anything from this header (std::endl is
declared in said:
As a rule of thumb try avoiding using directives. And always avoid
them at global or namespace scope in a file which gets #included
(because, that way, you inject a bunch of names in an unknown context:
whatever file happens to make the inclusion).
const int n = 100;
void HornerD(char[], int, int);
In C++ you have better alternatives here than char pointer (note, too,
that even using a char pointer it would have been much better to
declare it as
const char *
)
Move this as close as possible to where it is first assigned to (i.e.
the cin >> x; statement). I'd also heartedly recommend to use a name
such as 'base'. Do not underestimate the importance of names.
A comment similar to the one above. Prefer std::string. Also avoid
all-uppercase names for variables, because they are generally used for
macros (though, being this a one-letter, it shouldn't be defined as a
macro either by any sane code)
cout<<"Enter a number: ";
cin>>P;
int n=strlen(P);
Now you are shadowing the constant defined above (which has no reason
to exist, in fact). Also you don't include any header for strlen. But
you don't need strlen at all if you use std::string.
cout<<"Enter the base: ";
cin>>x;
HornerD(P, n, x);
return 0;
}
void HornerD(char *a, int n, int x)
{
int *p;
p=new int[n];
Memory leak. You never delete the pointed to array. And there's no
reason to separate the declaration: just write (when you really need
to new[]) int * p = new int[ count ];
Wrong. For characters through '0'-'9' you are guaranteed that the
numerical value of c - '0' corresponds to c. So, had you written
p[ 0 ] = a[ 0 ] - '0';
that would have been correct, but *only* for characters in '0'-'9'.
This is of course the main reason why the program gives incorrect
results for base 16.
Note, too, that in a base b you can only enter digits between 0 and
b-1, so you might want to validate the input (and probably ask the
user to enter the base first, then the number)
for(int i=0; i<n; i++)
p[i+1]=p*x + (a[i+1]-48);
Same error as above. (Also, never hardcode constants)
cout << "Result in decimal is: "<<p[n-1]<<endl;
Note, too, that you are mixing two responsibilities in the HornerD
function: 1) calculating the number (decimal representation) 2)
displaying it.
This surprises the user (would you suppose that a function called
HornerD displayed something?) and prevents code reuse.
For starters, change it to something like
int HornerD( const std::string & representation,
int base );
(supposing that an int is large enough for storing the result: that's
a consideration you should make in advance)