446,260 Members | 1,338 Online Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

# Problem in the convertion of any number base B to decimal

 P: n/a Hi, the program is asking the user for a number ant he base of the number, then it will convert the number to decimal. But is running right only with base 2, 3, 4, 5, 6, 7, 8 but if I try 16 it is not working. can some one help me please. Thanks #include #include using namespace std; const int n = 100; void HornerD(char[], int, int); int main() { int x; char P[n]; cout<<"Enter a number: "; cin>>P; int n=strlen(P); 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]; p=a-48; for(int i=0; i
8 Replies

 P: n/a #include using namespace std; const int n = 100; void HornerD(char[], int, int); int main() { int x; char P[n]; cout<<"Enter a number: "; cin>>P; int n=strlen(P); 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]; p=a-48; for(int i=0; i

 P: n/a On Fri, 08 Jun 2007 19:33:44 -0700, sd****@gmail.com wrote: >Hi, the program is asking the user for a number ant he base of thenumber, 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 try16 it is not working. At a glance the case base = 9 shouldn't be any different, though I haven't run the program. [...] >#include Strictly speaking you're better off also #including . >#include AFAIK, you're not using anything from this header (std::endl is declared in ) >using namespace std; 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 * ) >int main(){ int x; 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. > char P[n]; 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 ]; p=a-48; 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} -- Gennaro Prota -- Need C++ developers? I'm available https://sourceforge.net/projects/breeze/ (replace 'address' with 'name.surname' to mail) Jun 9 '07 #3

 P: n/a On Sat, 09 Jun 2007 13:44:50 +0200, Gennaro Prota wrote: >On Fri, 08 Jun 2007 19:33:44 -0700, sd****@gmail.com wrote: [...] >for(int i=0; i

 P: n/a On Jun 9, 1:44 pm, Gennaro Prota Strictly speaking you're better off also #including . That used to be the case, but at the last meeting, the committee approuved a resolution to make = 2 && base <= 36. for(int i=0; i= 2, or even too much about the 36 in base <= 36, above. -- James Kanze (Gabi Software) email: ja*********@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 Jun 9 '07 #5

 P: n/a Ok, I got it know. But how about if the user enter 1011.101 base 2. Expand|Select|Wrap|Line Numbers #include  #include  using namespace std;   const int c = 100; void HornerD(char[], int, int);   int main() { int x; char P[c];   cout<<"Enter a number: "; cin>>P; int n=strlen(P); cout<<"Enter the base: "; cin>>x;   HornerD(P, x, n); return 0; }   void HornerD(char *a, int x, int n) { int p = 0;   if(x 0 && x <= 9) { for (int i=0; i='A') p = p * x + (a[i] - 'A'+10); else p=p*x+(a[i]-'0'); } }   cout << "Result in decimal is: "<

 P: n/a Sorry this is my new code. But how about if the user enter 1011.101 base 2. #include #include using namespace std; const int c = 100; void HornerD(char[], int, int); int main() { int x; char P[c]; cout<<"Enter a number: "; cin>>P; int n=strlen(P); cout<<"Enter the base: "; cin>>x; HornerD(P, x, n); return 0; } void HornerD(char *a, int x, int n) { int p = 0; for (int i=0; i='A') p = p * x + (a[i] - 'A'+10); else p=p*x+(a[i]-'0'); } cout << "Result in decimal is: "<

 P: n/a > #include #include // using namespace std; > const int c = 100; void HornerD(char[], int, int); int main(){ using std::cout; using std::cin; // if you simply must! cout<<"Enter a number: "; char P[c]; cin>>P; How do you know 'P' is valid here? int n = strlen( P ); cout<<" You entered "<>x; if( not x ){ std::cerr"The RedCoats are coming!!"< HornerD(P, x, n); return 0; } // main() void HornerD( char *a, int x, int n ){ int p = 0; for( int i=0; i < n; ++i ){ if( a[ i ] == '.' ){ std::cerr<<"Son-of-a-bitch!!!! What now!?!"<= 'A' ) p = p * x + ( a[ i ] - 'A' + 10 ); What is '10', other than a "magic-number"? I bet it would like a name! What if x == 3458967328-67398? else p = p * x + ( a[ i ] - '0' ); } // for(i) cout << "Result in decimal is: "< 