Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Following program doesn't give the output..
Please tell where am i going wrong...
I have written the program logic in first few lines, Convert a number of maximum ten digit from any base to any base...( base range :2-20)
// This program converts given no. to decimal.
// then it converts decimal no. to source base.
//checks if the new no. and initial no. are same
//if same, converts the decimal no. into target base
//if not, again asks for input
#include<stdio.h>
#include<conio.h>
#include<math.h>          //for pow function
#include<string.h>        //for strcmpi

long int cnvrtdeci(char *, int);              //convert into decimal
int chkbase(long int, int, char*);            //checks base
char* cnvrt(long int,int);                    //convert into target base

void main()
{
    char num[11],*p;
    long int m;
    int base=0,trgt, c,i;
    clrscr();
    back:
    printf("\nInput:");
    scanf("%s", num);
    scanf("%d %d", &base, &trgt);
    m=cnvrtdeci(&num[0], base);
    c=chkbase(m, base, &num[0]);
    if(c==1)
    {
       printf("\nWrong input!!");
       goto back;
    }
    p = cnvrt(m,trgt);
    printf("\nOutput:");
    for (; *p!='\0';p++)
       printf("%s", *p);
    getch();
}
long int cnvrtdeci(char *j,int b)
{
    char *i;
    long int sum=0;
    int k;
    while( *j != '\0' )
      j++;
    for( i=j-1,k=0; i>=0; i--,k++)
    {
      switch(*i)
      {
        case 'A':
        case 'a': sum+=10 * pow(b,k);
              break;
        case 'B':
        case 'b': sum+=11 * pow(b,k);
              break;
        case 'C':
        case 'c': sum+=12 * pow(b,k);
              break;
        case 'D':
        case 'd': sum+=13 * pow(b,k);
              break;
        case 'E':
        case 'e': sum+=14 * pow(b,k);
              break;
        case 'F':
        case 'f': sum+=15 * pow(b,k);
              break;
        case 'G':
        case 'g': sum+=16 * pow(b,k);
              break;
        case 'H':
        case 'h': sum+=17 * pow(b,k);
              break;
        case 'I':
        case 'i': sum+=18 * pow(b,k);
              break;
        case 'J':
        case 'j': sum+=19 * pow(b,k);
              break;
        default: sum+= (*j) * pow(b,k);
       }

    }
    return sum;
}
int chkbase( long int m,int base,char *p)
{
    char *j;
    int t;
    j= cnvrt(m,base);
    t= strcmpi ( j,p);
    if( t!=0)
         return 1;
    else
         return 0;
}

char* cnvrt( long int m, int t)
{
    char res[50];
    int r,j;
    for( j=48;m>=0;j--)
    {
      r= m%t;
      m= m/t;
      if ( r>=0 && r<=9 )
       res[j]=r;
      else
       switch(r)
       {
         case '10': res[j] = 'A';
            break;
         case '11': res[j] = 'B';
            break;
         case '12': res[j] = 'C';
            break;
         case '13': res[j] = 'D';
            break;
         case '14': res[j] = 'E';
            break;
         case '15': res[j] = 'F';
            break;
         case '16': res[j] = 'G';
            break;
         case '17': res[j] = 'H';
            break;
         case '18': res[j] = 'I';
            break;
         case '19': res[j] = 'J';
            break;
         }
      }
         return &res[0];
}
Posted
Updated 24-Apr-11 9:29am
v2
Comments
shyla123 24-Apr-11 15:31pm    
thnx...
pls solve my doubt too..

1 solution

It's difficult to be certain what the problem you have is, since you don't really tell us: "program doesn't give the output" is not exactly diagnostic.

So: I'll explain a couple of obvious nasties, and then move on to how to cure it. Be aware though, that I am not going to fix it for you - it's your homework, it's your job to fix it!

1) Minor, but important. Don't just prompt the user for "Input:". Ask him to enter the number to convert, the base it is in, and then the base he wants. A lot nicer for the user!
2) Goto. Please remove this from your code, and forget for two years that it ever existed. After that, you should understand why beginners should never use it. Trust me on this.
3) This code may never exit. Or at least, not in a reasonable timescale.
for( j=48;m>=0;j--)
{
  r= m%t;
  m= m/t;
Why not? Answer: what is the result of zero divided by a non-zero number?
4) Please, do yourself a favour: rename your variables to be descriptive of what they actually are there for. Using single character names save you typing, but in two weeks time, it will cause serious head-scratchiong as you try to work out if j is supposed to be the interim result of trying a conversion to a base, or a loop counter. Again, trust me on this!

Now: fixing it.

Kick up the debugger.
Place a breakpoint on the first line after the two scanf calls:
m=cnvrtdeci(&num[0], base);

Run your program until the breakpoint is hit.
Look at you variables "num" and "base". Work out in your own mind what value should be in "m" after cnvrtdeci has been called. Now single step the debugger over the call. Is the value of "m" what you thought it would be?
If not, why is it different? Restart the program, and this time step into the routine.

Keep doing this: Work out what you expect, then see if you get it. If you do, good! If you don't, why not?
You will be doing this a lot in development - it is well worth getting comfortable doing it now!
Good luck!
 
Share this answer
 
Comments
Albert Holguin 24-Apr-11 16:53pm    
my 5
Sergey Alexandrovich Kryukov 24-Apr-11 22:41pm    
Good points, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900