Ok the admin said to me not to post in C++ if my problem is in C language. Ok here's my problem with my program now. #1 If I put this in a array what should I change? #2 In my looping I needed to press double enter to enter in a loop but I only needed to press once the enter. #3 If I exit the program and I run it again the program will not clear screen it will show the text that the program shown in the last run. #4 Is my fucntion calling correct? NOTE THIS IS "TURBO C++ COMPILER"

#include<conio.h>
#include<stdio.h>
#include<math.h>

char base;
char key;

void Screen_Header();
void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();

int  main()
     {
   Screen_Header();
   Binary2Decimal();
   Octal2Decimal();
   Hexa2Decimal();
   return 0;
     }

void Screen_Header()
       {
       do{
       clrscr();
       gotoxy(1,3);printf("Conversion from any base to base 10");
       gotoxy(1,5);printf("a - Binary");
       gotoxy(1,6);printf("b - Octal");
       gotoxy(1,7);printf("c - Hexadecimal");
       gotoxy(1,11);printf("Select a base to be converted: ");
       scanf("%c", &base);

       switch(base){
       case 'a':
       Binary2Decimal();
       break;
       case 'b':
       Octal2Decimal();
       break;
       case 'c':
       Hexa2Decimal();
       break;
       }

       gotoxy(1,20);printf("Press <DOUBLE ENTER> to Continue or Press <ANY KEY> to Exit ");
       key=getch();
       }

       while(key == 13);
       }


void Binary2Decimal()
       {
       gotoxy(1,13);printf("[BINARY TO DECIMAL CONVERSION]");

       int bin2,f2=1,d2=0;
       gotoxy(1,15);printf("Enter a Binary number: ");
       scanf("%d", &bin2);

       printf("\n");

       while(bin2>0)
       {
       if((bin2%10)==1)
       {
       d2=d2+f2;
       }
       bin2=bin2/10;
       f2=f2*2;
       }
       printf("Decimal equivalent is: %d", d2);
       }

void Octal2Decimal()
       {
       gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");

       long oct8,dec8,rem8,i8,sum8;
       gotoxy(1,15);printf("Enter an Octal number: ");
       scanf("%o", &oct8);

       printf("\n");

       {
       rem8=dec8%8;
       sum8=sum8 + (i8*rem8);
       }
       printf("Decimal equivalent is: %d", oct8);
       }

void Hexa2Decimal()
       {
       gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");

       long hex16,dec16,rem16,i16,sum16;
       gotoxy(1,15);printf("Enter a Hexadecimal number: ");
       scanf("%X", &hex16);

       printf("\n");

       {
       rem16=dec16%16;
       sum16=sum16 + (i16*rem16);
       }
       printf("Decimal equivalent is: %d", hex16);
       }

Recommended Answers

All 48 Replies

I posted one last message in the previous thread, if you haven't read it yet please do so before doing anything else.

As for the language issue, I suspected that this was going to be the case. It was one of the reasons I kept asking about that earlier.

Follow Schol-R-Lea's advice regarding fixing the conversion functions.
For the code you posted above, remove the do while loop from void Screen_Header() and simplify it to:

void Screen_Header()
{
    clrscr();
    gotoxy(1,3);
    printf("Conversion from any base to base 10");

    gotoxy(1,5);
    printf("a - Binary");

    gotoxy(1,6);
    printf("b - Octal");

    gotoxy(1,7);
    printf("c - Hexadecimal");

    gotoxy(1,11);
    printf("Select a base to be converted: ");

    scanf("%c", &base);
    switch(base){
    case 'a':
        Binary2Decimal();
        break;
    case 'b':
        Octal2Decimal();
        break;
    case 'c':
        Hexa2Decimal();
        break;
    }
}

Then put the do while loop and prompts to continue into the main function:

int  main()
{
    do {
        Screen_Header();

        fflushStdin();

        printf("Press <ENTER> to Continue or Press <ANY KEY> to Exit ");

    } while (_getch() == '\r');

    return 0;
}

Where should I put my switch case sir? Hmmm sir I did not change anything yet to my program. My problem with my program is that enter and exit. I needed to press "Enter" twice but that is wrong and my "Exit" it doesn't exit properly. And my conversion needs to be in an array.

One more thing I want to change my switch case default to ---> if condition. How to that?

I went to the extraordinary step of setting up Tubo C++ 1.01 in DosBox on my system, and was able to get the following program to run correctly. Thry it an see if it is acceptable to both TC++ and your professor.

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void Screen_Header();
void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();

int  main()
{
    char key;

    do
    {    
        Screen_Header();

        scanf("%c", &key);
        gotoxy(1,20);
        printf("Press <ENTER> to Continue or Press <ANY KEY> to Exit ");
        key = getch();
    }
    while(key == '\r');

    return 0;
}

void Screen_Header()
{
    char base;

    clrscr();
    gotoxy(1,3);
    printf("Conversion from any base to base 10");
    gotoxy(1,5);
    printf("a - Binary");
    gotoxy(1,6);
    printf("b - Octal");
    gotoxy(1,7);
    printf("c - Hexadecimal");
    gotoxy(1,11);
    printf("Select a base to be converted: ");
    scanf("%c", &base);

    switch(base)
    {
    case 'a':
        Binary2Decimal();
        break;
    case 'b':
        Octal2Decimal();
        break;
    case 'c':
        Hexa2Decimal();
        break;
    default:
        printf("Sorry, %c is not a valid menu option", base);
    }
}


void Binary2Decimal()
{
    int bin2,f2=1,d2=0;

    gotoxy(1,13);
    printf("[BINARY TO DECIMAL CONVERSION]");
    gotoxy(1,15);
    printf("Enter a Binary number: ");
    scanf("%d", &bin2);

    printf("\n");

    while(bin2>0)
    {
        if((bin2%10)==1)
        {
            d2=d2+f2;
        }
        bin2=bin2/10;
        f2=f2*2;
    }
    printf("Decimal equivalent is: %d", d2);
}

void Octal2Decimal()
{
    char oct8[12];
    unsigned long dec8,i8,sum8,len8;

    sum8 = 0;
    gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter an Octal number: ");
    scanf("%11s", oct8);
    printf("\n");

    len8 = strlen(oct8);
    for (i8 = 0; i8 < len8; ++i8)
    {
        dec8 = oct8[i8] - '0';  /* get the value of the digit */
        sum8 = (sum8 * 8) + dec8;
    }
    printf("Decimal equivalent is: %u", sum8);
}


void Hexa2Decimal()
{
    char hex16[9];
    unsigned long dec16,i16,sum16,len16;

    sum16 = 0;
    dec16 = 0;
    gotoxy(1,13);printf("[HEX TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter an Hex number: ");
    scanf("%8s", hex16);
    printf("\n");

    len16 = strlen(hex16);
    for (i16 = 0; i16 < len16; ++i16)
    {
        printf("%c\n", hex16[i16]);
        if (isdigit(hex16[i16]))
        {
            dec16 = hex16[i16] - '0';  /* get the value of the digit */
        }
        else
        {
            dec16 = toupper(hex16[i16]) - 'A' + 10;
        }

        sum16 = (sum16 * 16) + dec16;
    }
    printf("Decimal equivalent is: %u", sum16);
}

Sir your the best :) !!! Sir just one more thing can you explain the codes you used to change some of tweaks of my program why you used them, what's the function of the codes and etc...? The codes she didn't teach are the following "unsigned long, %11s , %8s , %u, strlen and etc...

Hmmm sir why is it Hexa2Decimal like this? In my Binary2Decimal & Octal2Decimal are ok.

Hmmm sir why is it Hexa2Decimal like this? In my Binary2Decimal & Octal2Decimal are ok.

Comment out line 123: printf("%c\n", hex16[i16]);

Comment it out to make sure that it fixes the display problem. If everything is OK, then delete it if you like.

It's ok now :). Sir what is '\r' Im not familiar with it.

hmm sir I i removed printf("%c\n", hex16[i16]); but now my conversion of my hexa2decimal are now wrong :( . How to fix this? Well my 3 conversion prgram will work properly if I only put 4 digit other than that nothings else. I want to input 10 digits as maximum but the conversion is still correct.

From the screen shot you posted above, it appears that the size of an unsigned long is only 16 bits (2 bytes) for the Turbo compiler you have.
Therefore, when you entered hex: 233AC it was truncated to two bytes 33AC = 13228 decimal.

Oops. Sorry about that debug-print line I left in there.

OK, I've checked into this and found that I was wrong about the word sizes in Turbo C++. I knew that the int values were one word (16 bits) wide; but I thought that the long would be a double word 32 bits. Apparently not. With a only a 16 bit long value, the largest number you can have is 65535(base 10), which in hex is FFFF. By comaprison, the largest 32-bit number would be FFFFFFFF(base 16), or 4294967295(base 10), or 37777777777(base 8). You would need a 64-bit long to hold more than eight hex digits (well, actually, 39 bits would be sufficient, but the x86 processors all have word sizes that double at each step up - 8, 16, 32, and 64). Given that each byte requires two hex digits to represent it, a quadword value (64 bits, or 8 bytes) can hold 16 hex digits, naturally enough.

An unsigned value is one is always interpreted as a positive number, rather than treated values which have the last bit set as negative numbers. It let's you represent larger positive values, but at the expense of repreenting negative values.

As for why I used an unsigned long instead of a signed long, that's because if you entered a value greater than half the maximum value allowed in an unsigned long, it would come out as a negative value. The Intel processor (and most others today) used two's complement to represent the sign of a number. A negative value is equal to the complement of it's binary value, plus one; This means that the last bit in the number - bit 15, in this case - is always set in a negative number, and never in a positive one, if the number is being treated as signed. for example, if you have an 8-bit short value of 5,

00000101

it's complement would be

11111010

which, when you add 1, becomes

11111011

which is the representation of -5 in two's complement. BUT, if you treat it as an unsigned value, it is interpreted as 251.

As for the '\r', it is a character code for the carriage return, one of the two parts of a newline. It is equivalent to the ASCII value 13(base 10).

Ok I get now. Sir how to fix the hexa2decimal? I want to remove the debug prints but If I remove it the conversion will be wrong.

All you need to do is delete or comment out that one line. This should do fine:

void Hexa2Decimal()
{
    char hex16[9];
    unsigned long dec16,i16,sum16,len16;

    sum16 = 0;
    dec16 = 0;
    gotoxy(1,13);printf("[HEX TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter an Hex number: ");
    scanf("%8s", hex16);
    printf("\n");

    len16 = strlen(hex16);
    for (i16 = 0; i16 < len16; ++i16)
    {

        if (isdigit(hex16[i16]))
        {
            dec16 = hex16[i16] - '0';  /* get the value of the digit */
        }
        else
        {
            dec16 = toupper(hex16[i16]) - 'A' + 10;
        }

        sum16 = (sum16 * 16) + dec16;
    }
    printf("Decimal equivalent is: %u", sum16);
}

Why is the Exit option is not with the void Screen_Header? How do I change my "Switch,Case & Default" -------> "If statement"?

Ok my program is done thank you very much guys :)) Some of the codes I don't understand like the formula of the 3 conversion because I just get this from google. And some of the logic of this codes and the structure of the program. Because I'm defending this program tomorrow I need all the information I can gather so I can answer what any question what my prof throw at me tomorrow.

#include<conio.h>
#include<stdio.h>
#include<math.h>

char base;
char key;

void Screen_Header();
void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();

void main()
     {
       do{
       clrscr();
       gotoxy(1,3);printf("Conversion from any base to base 10");
       gotoxy(1,5);printf("a - Binary");
       gotoxy(1,6);printf("b - Octal");
       gotoxy(1,7);printf("c - Hexadecimal");
       gotoxy(1,11);printf("Select a base to be converted: ");
       scanf("%c", &base);

       if((base=='a')||(base=='A')){
       Binary2Decimal();  }
       if((base=='b')||(base=='B')){
       Octal2Decimal();   }
       if((base=='c')||(base=='C')){
       Hexa2Decimal();    }

       gotoxy(1,20);printf("Press <ENTER> to Continue or Press <ANY KEY> to Exit");
       scanf("%c", &key);
       key = getch();
       }
       while(key == 13);
       }

void Binary2Decimal()
       {
       gotoxy(1,13);printf("[BINARY TO DECIMAL CONVERSION]");

       int bin2,f2=1,d2=0;
       gotoxy(1,15);printf("Enter a Binary number: ");
       scanf("%d", &bin2);

       printf("\n");

       while(bin2>0)
       {
       if((bin2%10)==1)
       {
       d2=d2+f2;
       }
       bin2=bin2/10;
       f2=f2*2;
       }
       printf("Decimal equivalent is: %d", d2);
       }

void Octal2Decimal()
       {
       gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");

       long oct8,dec8,rem8,i8,sum8;
       gotoxy(1,15);printf("Enter an Octal number: ");
       scanf("%o", &oct8);

       printf("\n");

       {
       rem8=dec8%8;
       sum8=sum8 + (i8*rem8);
       }
       printf("Decimal equivalent is: %d", oct8);
       }

void Hexa2Decimal()
       {
       gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");

       long hex16,dec16,rem16,i16,sum16;
       gotoxy(1,15);printf("Enter a Hexadecimal number: ");
       scanf("%X", &hex16);

       printf("\n");

       {
       rem16=dec16%16;
       sum16=sum16 + (i16*rem16);
       }
       printf("Decimal equivalent is: %d", hex16);
       }

Sir how can I extend the bit of my 3 conversions? Because in my Binary2Decimal & Octal2Decimal can only accept 5 digits to convert it won't accept 6 to 10 digits. And my Hexa2Decimal will only accept 4 digits first number example 23AC=9132, but it won't accept 4 digits first letter AC23=-21469 that is so wrong. How to fix this?

AC23 does equal -21469 when the size of an integer is only 16 bits. Any value from 0x8000 to 0xFFFF will have its most significant bit set, meaning it will have a negative value.

To accept larger input for the other conversions, you would need to get the input as a string and validate what has been entered. For example, in the Binary2Decimal function, the only valid input is 0s and 1s.
Here's an example of the binary conversion using the above.

void Binary2Decimal()
{
    char buffer[17];
    int len, i, total = 0, value;

    printf("[BINARY TO DECIMAL CONVERSION]\n\n");
    printf("Enter a Binary number: ");
    // accept a maximum of 16 chars
    scanf("%16s", buffer);
    printf("\n");

    len = strlen(buffer);

    // convert and check if we have valid data
    for (i = 0; i < len; ++i)
    {
        value = buffer[i] - '0';
        if (value != 0 && value != 1)
        {
            printf("Invalid binary data\n");
            return;
        }
        total = (total << 1) | value;   // (total * 2) + value
    }
    printf("Decimal equivalent is: %d\n", total);
}

Hmmm how about the Octa2Decimal & Hexa2Decimal? And can you explain this codes to me.

Sir how about doing the same of Octal2Decimal & Hexa2Decimal.

Fortunately, the code for the octal function is very similar to that for the binary:

void Octal2Decimal()
{
    char buffer[7];
    int len, i, total = 0, value;

    printf("[OCTAL TO DECIMAL CONVERSION]\n\n");
    printf("Enter an Octal number: ");
    // accept a maximum of 6 chars
    scanf("%6s", buffer);
    printf("\n");

    len = strlen(buffer);

    // convert and check if we have valid data
    for (i = 0; i < len; ++i)
    {
        value = buffer[i] - '0';
        if (value < 0 && value > 7)
        {
            printf("Invalid octal data\n");
            return;
        }
        total = (total * 8) + value
    }
    printf("Decimal equivalent is: %d\n", total);
}

What it does is read the input string into buffer[], and then for each digit, it subtracts the ASCII character value of the digit from the ASCII value of '0'. Since the ASCII values for numerals is in order 0 to 9, this has the effect of getting the numeric value of the digit.

It then checks whether the value is in the value range of an octal digit, that is to say, between 0 and 7. If it itsn't, it prints an error message and ends the function.

finally, if the digit is valid, it multiplies the running total by the base of the representation - eight, in this case - to 'push' the existing values up one octal place, so that if the previosu total was, for example, '417' (base 8), then the new total would be '4170' (base 8). It then adds the digit's value to the total. Since the digit is guaranteed to be between zero and seven, this means that the value will 'fit' exactly into the octal place it was moved by. So, if the digit was '3', the running total would be '4173' (base 8).

The hexadecimal code is a little more involved, as it has to deal with two distinct ranges of ASCII codes: '0' through '9' and 'A' through 'F'. We can do this as follows:

    void Hexadecimal2Decimal()
    {
        char buffer[5], digit;
        int len, i, total = 0, value;

        printf("[HEX TO DECIMAL CONVERSION]\n\n");
        printf("Enter an Hex number: ");
        // accept a maximum of 4 chars
        scanf("%4s", buffer);
        printf("\n");

        len = strlen(buffer);

        // convert and check if we have valid data
        for (i = 0; i < len; ++i)
        {
            digit = toupper(buffer[i]);
            if (digit >= '0' && digit <= '9')
            {
                value =  digit - '0';
            }
            else if (digit >= 'A' && digit <= 'F')
            {
                value = digit - 'A' + 10;
            }
            else
            {
                printf("Invalid hexadecimal data\n");
                return;
            }
            total = (total * 16) + value
        }
        printf("Decimal equivalent is: %d\n", total);
    } 

The major change here is that we start off by converting the character to an uppercase valuewhich will make it easier if it turns out to be a letter. We then check to see if the digit is a numeral, that is, in the range between '0' and '9'. If it is, we convert it as before; if not, we check to see if it is in the range 'A' to 'F', and if it is, we subtract 'A' from it and add ten - because the values from 'A' to 'F' start at decimal 10 (e.g., A(base 16) is equal to 10 (base 10) and so forth). If it is in neither range, we print the error. The rest is the same as before, just using base 16.

Why is my Octal2Decimal & Hexa2Decimal like this I just insert your codes why conversion is not correct and why is my Octal2Decimal like that? How to fix this?

The conversion of my Hexa2Decimal is wrong I just copy your codes. How to fix this?

I know it doesn't sound relevant here but you still aren't indenting your code in a useful manner. The last function should look like:

void Hexa2Decimal()
{
    char hex16[17];
    unsigned long dec16,i16,sum16,len16;

    sum16=0;
    dec16=0;

    gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");
    gotoxy(1,15);printf("Enter a Hexadecimal number: ");
    scanf("%16s", hex16);
    printf("\n");

    len16=strlen(hex16);
    for(i16=0;i16<len16;++i16)
    {
        if(isdigit(hex16[i16]))
        {
            dec16=hex16[i16]-'0';
        }
        else
        {
            dec16=toupper(hex16[i16])-'A'+10;
        }
        sum16=(sum16*16)+dec16;
    }
    printf("Decimal equivalent is: %d\n", sum16);
}

If you look, this version of the code indents every time you enter a block, and de-dents when you leave the block. As I explained here, the whole purpose of indentation like this is to make the nesting of the different conditional blocks obvious just at a glance.

As for the error, it looks as if only the final digit is being converted for some reason. Let me look at it some more and I'll get back to you.

Why is the indentation relevant? Because it would have made the problem with the ocatal function clear immediately - you have the printf() statement inside the loop, instead of after it.

Well here's my program now. I just insert your codes to my program.

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>

char base;
char key;

void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();

void main()
     {
       do{
       clrscr();
       gotoxy(1,3);printf("Conversion from any base to base 10");
       gotoxy(1,5);printf("a - Binary");
       gotoxy(1,6);printf("b - Octal");
       gotoxy(1,7);printf("c - Hexadecimal");
       gotoxy(1,11);printf("Select a base to be converted: ");
       scanf("%c", &base);

       if((base=='a')||(base=='A')){
       Binary2Decimal();  }
       if((base=='b')||(base=='B')){
       Octal2Decimal();   }
       if((base=='c')||(base=='C')){
       Hexa2Decimal();    }
       if((base!='a')&&(base!='A')&&(base!='b')&&(base!='B')&&(base!='c')
       &&(base!='C')){gotoxy(32,11);printf("Wrong letter"); }

       gotoxy(1,20);printf("Press <ENTER> to Continue or Press <ANY KEY> to Exit");
       scanf("%c", &key);
       key = getch();
       }
       while(key == 13);
       }

void Binary2Decimal()
       {
       char buffer[17];
       int len,i,total=0,value;
       gotoxy(1,13);printf("[BINARY TO DECIMAL CONVERSION]");
       gotoxy(1,15);printf("Enter a Binary number: ");  //accept a maximum of 16 chars
       scanf("%16s", buffer);
       printf("\n");

       len=strlen(buffer);
       for(i=0;i<len;++i)
      {
        value=buffer[i]-'0';
        if (value!=0 && value!=1)
         {
          printf("Invalid binary data\n");
          return;
        }
       total=(total<<1|value);
      }
       printf("Decimal equivalent is: %d\n", total);
       }

void Octal2Decimal()
       {
       char buffer[7];
       int len,i,total=0,value;

       gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");
       gotoxy(1,15);printf("Enter an Octal number: ");
       scanf("%6s", buffer);
       printf("\n");

       len=strlen(buffer);
       for(i=0;i<len;++i)
        {
         value=buffer[i]-'0';
         if(value< 0 && value > 7)
          {
           printf("Invalid octal data\n");
           return;
         }
     total=(total<<8)|value;
     printf("Decimal equivalent is: %d\n", total);
     }
       }

void Hexa2Decimal()
       {
       char buffer[5],digit;
       int len,i,total=0,value;

       gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");
       gotoxy(1,15);printf("Enter a Hexadecimal number: ");
       scanf("%4s", buffer);
       printf("\n");

       len=strlen(buffer);
       for(i=0;i<len;++i)
        {
         digit=toupper(buffer[i]);
         if(digit>='0' && digit<='9')
        {
          value=digit - '0';
          }
        else if(digit>= 'A' && digit<= 'F')
         {
     value=digit - 'A'+10;
        }
       else
      {
       printf("Invalid hexadecimal data\n");
       return;
    }
       total=(total<<16)+value;
       }
       printf("Decimal equivalent is: %d\n", total);
       }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.