Hi really need help. I'm new to programming and my teacher just gave a problem program "Conversion from any base to base 10" . So search in google and find this source code. I just combined the source code. This will run if it not combines. But I want this to be combined properly and I need this to be in character array and in for loop. So I really need about this :((((

#include<stdio.h>
#include<math.h>
#include<string.h>
int binary_decimal(int n);
int decimal_binary(int n);
int decimal_octal(int n);
int octal_decimal(int n);
void decimal_hex(int n, char hex[]);
int hex_decimal(char hex[]);
int main()
{
int n;
char c;
printf("Instruction:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal. \n");
printf("2. Enter alphabet 'b' to convert decimal to binary. \n");
scanf("%c",&c);
if (c =='d' || c=='D')
{
printf("Enter a binary number: ");
scanf("%d",&n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c=='B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}

int decimal_binary(int n) /* Function to convert decimal binary. */
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*1;
i*=10;
}
return binary;
}
int i,rem;

int binary_decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal=0; i=0; rem;
while (n!=0)
{
rem=n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
int n;
char c;
printf("Instruction:\n");
printf("1. Enter alphabet 'o' to convert decimal to octal. \n");
printf("2. Enter alphabet 'd' to convert octal to decimal.\n");
scanf("%c",&c);
if (c =='d' || c =='D')
{
printf("Enter an octal: ");
scanf("%d",&n);
printf("%d in octal = %d in decimal", n, octal_decimal(n));
}
if (c =='o' || c=='O')
{
printf("Enter a decimal number: ");
scanf("%d",&n);
printf("%d in decimal = %d in octal", n, decimal_octal(n));
}
return 0;
}
int decimal_octal(int n) /* Function to convert decimal to octal */
{
int rem, i=1, octal=0;
while (n!=0)
{
rem=n%8;
n/=8;
octal+=rem*i;
i*=10;
}
return octal;
}
int octal_decimal(int n) /* Function to convert octal to decimal */
{

int decimal=0, i=0; rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(8,i);
++1;
}
return decimal;
}
char hex[20],c;
int n;
printf("Instruction:\n");
printf("Enter h to convert decimal to hexadecimal:\n");
printf("Enter d to to hexadecimal number to decimal:\n");
printf("Enter a character: ");
scanf("%c",&c);
if (c=='h' || c=='H')
{
printf("Enter decimal number: ");
scanf("%d",&n);
decimal_hex(n,hex);
printf("Hexadecimal number: %s",hex);
}
if (c=='d' || c=='D")
{
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Decimal number: %d",hex_decimal(hex));
}
return 0;
}

void decimal_hex(int n, char hex[]) /* Function to convert decimal to hexadecimal. */
{
int i=0,rem;
while (n!=0)
{
rem=n%16;
switch(rem)
{
case 10:
hex[i]='A';
break;
case 11:
hex[i]='B';
break;
case 12:
hex[i]='C';
break;
case 13:
hex[i]='D';
break;
case 14:
hex[i]='E';
break;
case 15:
hex[i]='F';
break;
default:
hex[i]=rem+'0';
break;
}
++i;
n/=16;
}
hex[i]='\0';
strrev(hex); /* Reverse string */
}
int hex_decimal(char hex[]) /* Function to convert hexadecimal to decimal. */
{
int i, length, sum=0;
for(length=0; hex[length]!='\0';++length);
for(i=0; hex[i]!='\0';++i,--length)
{
if(hex[i]>='0' && hex[i]<='9')
sum+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
sum+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
sum+=(hex[i]=87)*pow(16,length-1);
}
return sum;
}

You have just copied the code from somewhere in a hope that it will work? This is a very bad way to solve a problem, and, this has obviously been shown here.

From what I can see, skim reading of the code, you seem to have a mixture of C and C++ in here. Not great, but, also not the end of the world, providing you handle this correctly..

What I suggest that you do is, firstly sit down and THINK about what you want to do. Get a pen and piece of paper, perform some steps or calculations on what it is that you're trying to do and then formulate how this will be put i nto code.

Conversion from any base to base 10

Is that what your example tries to do?

No!

You may glean some ideas there ... though?

If you are to code it in C++ ...

best to start to do that.

Get a working shell that takes (some) input and gives the expected output.

Then work in steps from there.

Perhaps ... code a decimal to binary conversion first
using C++ string for input of the decimal and output of the correctly matching binary formatted the way you like.

Good to firstly validate that the input string contains only digits 0..9 (and not an empty string, either.)

I already did that. And it did not work.

Please show your working C++ code shell ... that takes in a valid C++ string and ...

Below is some working C++ demo code that inputs a C++ string and outouts each char ... this is just to demo a 'working shell' ... to get you started coding with C++ string.

// stringsCppTest2.cpp //


// http://developers-heaven.net/forum/index.php/topic,46.0.html

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string test;
    do
    {
        cout << "Enter 'test' string (empty string to exit) : " << flush;
        getline( cin, test );
        size_t len = test.size();
        for( size_t i = 0; i < len; ++i )
        {
            cout << "char at index [" << i << "] was " << test[i] << endl;
        }
    }
    while( test.size() );
}

Ok this is the first conversion I did and it is not functioning.

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int n;
char c;
printf("Instruction:\n");
printf("1. Enter alphabet 'd' to convert binary to decimal. \n");
printf("2. Enter alphabet 'b' to convert decimal to binary. \n");
scanf("%c",&c);
if (c =='d' || c=='D')
{
printf("Enter a binary number: ");
scanf("%d",&n);
printf("%d in binary = %d in decimal", n, binary_decimal(n));
}
if (c =='b' || c=='B')
{
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %d in binary", n, decimal_binary(n));
}
return 0;
}

int decimal_binary(int n) /* Function to convert decimal binary. */
{
int rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*1;
i*=10;
}
return binary;
}
int i,rem;

int binary_decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal=0; i=0; rem;
while (n!=0)
{
rem=n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}

A working code 'shell' is just a starting sequence of code that compiles and gives the exact expected output for all accepted input ...

You are still coding in C ...

What does 'not functioning' mean?

Does your code compile?

Does your code give any output?

If so, what part(s) of output are problematic?

I thought you wanted to code in C++

Below is a working C++ 'shell' that takes (only valid) C++ string input ... and gives some (the here expected output) to prove that it is really working.

Below is only just a 'first step' ...

    // stringsCppTest2.cpp //  // 2014-02-26 //


    // http://developers-heaven.net/forum/index.php/topic,46.0.html

    #include <iostream>
    #include <string>
    #include <cctype> // re. isdigit

    using namespace std;

    // valid if not empty and only contains digits 0..9 ...
    bool isValidInt( const string& n )
    {
        size_t len = n.size();
        if( !len ) return false;

        for( size_t i = 0; i < len; ++i )
            if( !isdigit( n[i] ) ) return false;
        // else ...
        return true;
    }

    int main()
    {
        string test;
        do
        {
            cout << "Enter 'test' string (empty string to exit) :  " << flush;
            getline( cin, test );

            if( !isValidInt( test ) )
            {
                cout << "\nNot a valid integer!  Try again ... \n";
                continue; // jump to while test at end of loop right now ...
            }

            size_t len = test.size();
            for( size_t i = 0; i < len; ++i )
            {
                cout << "char at index [" << i << "] was " << test[i] << endl;
            }
        }
        while( test.size() );
    }

Im using turbo c++ and the prototype iostream,string and cctype won't work without an .h

And there's a big part of your problem. You're using a 30 year old compiler, no doubt older than you are yourself, and expect it to work properly with new versions of the language on new computers with new operating systems.
Use something reasonably up to date instead, or find yourself a 286 based computer running DOS and the programming manuals to go with it, then try again using that.

But our prof specify to use only turbo c++ and I don't know any up to date c++ software.

Well ... that was insightful :)

Maybe ... you could switch to the C forum ... and do the code all in C?

But first? Do you have C++ strings ? Or do you need a C++ class String to use?

I need a C++ class string. Because obviosly my compiler is to old. Sorry I didn't mension earlier about my compiler :(

How about this is this correct?

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

void main(){

    char value;
    char t, u, v;
    char answer;

    do{

    clrscr();

    cout<<"**********NUMBER CONVERSION**********"<<endl;
    cout<<endl;

    cout<<"a - Binary"<<endl;
    cout<<"b - Octal"<<endl;
    cout<<"c - Decimal"<<endl;
    cout<<"d - Hexadecimal"<<endl;
    cout<<endl;

    cout<<"Select a value to be converted: ";
    cin>>value;

    switch(value){

    case 'a':
        cout<<endl<<"[BINARY CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Octal"<<endl;
        cout<<"b - Decimal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>t;

        switch(t){

            case 'a':                           //ok
                cout<<endl<<"[BINARY TO OCTAL CONVERSION]"<<endl;
                cout<<endl;

                long b1,f1=1,d1=0,num5,result5[8]={0,0,0,0,0,0,0,0},flag5=0;
                cout<<"Enter a Binary number: ";
                cin>>b1;

                cout<<"\n\n";

                while(b1>0)
                {
                   if((b1%10)==1)
                   {
                      d1=d1+f1;
                   }
                   b1=b1/10;
                   f1=f1*2;
                }

                num5=d1;
                cout<<"Octal equivalent is: ";
                for(int i5=0;i5<8;++i5)
                {
                   result5[7-i5]=num5%8;
                   num5=num5/8;

                   if(num5==0)
                      break;
                }

                for(i5=0;i5<8;++i5)
                {
                   if(result5[i5]!=0)
                      flag5=1;

                   if(flag5==1)
                      cout<<result5[i5];
                }

                cout<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[BINARY TO DECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long b2,f2=1,d2=0;
                cout<<"Enter a Binary number: ";
                cin>>b2;

                cout<<"\n\n";

                while(b2>0)
                {
                   if((b2%10)==1)
                   {
                      d2=d2+f2;
                   }
                   b2=b2/10;
                   f2=f2*2;
                }

                cout<<"Decimal equivalent is: "<<d2<<endl;
                break;

            case 'c':                           //ok
                cout<<"[BINARY TO HEXADECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long b3,f3=1,d3=0,r6[10],z6=0,number6;
                cout<<"Enter a Binary number: ";
                cin>>b3;

                cout<<"\n\n";

                while(b3>0)
                {
                   if((b3%10)==1)
                   {
                      d3=d3+f3;
                   }
                   b3=b3/10;
                   f3=f3*2;
                }

                number6=d3;
                while(number6>0)
                {
                   r6[z6]=number6%16;
                   number6=number6/16;
                   z6++;
                }

                cout<<"Hexadecimal equivalent is: ";

                for(int j6=z6-1;j6>=0;j6--)
                {
                   if(r6[j6]==10)
                      cout<<"A";
                   else if(r6[j6]==11)
                      cout<<"B";
                   else if(r6[j6]==12)
                      cout<<"C";
                   else if(r6[j6]==13)
                      cout<<"D";
                   else if(r6[j6]==14)
                      cout<<"E";
                   else if(r6[j6]==15)
                      cout<<"F";
                   else
                      cout<<r6[j6];
                }

                cout<<endl;
                break;

            default:
                cout<<endl;
        }

        break;

    case 'b':
        cout<<endl<<"[OCTAL CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Binary"<<endl;
        cout<<"b - Decimal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>u;

        switch(u){

            case 'a':                           //ok
                cout<<endl<<"[OCTAL TO BINARY CONVERSION]"<<endl;
                cout<<endl;

                long number4,dec7,rem7,i7=1,sum7=0;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number4);

                cout<<"\n\n";

                dec7=printf("%d", number4);

                while(dec7>0)
                {
                   rem7=dec7%2;
                   sum7=sum7 + (i7*rem7);
                   dec7=dec7/2;
                   i7=i7*10;
                }

                cout<<"Binary equivalent is: "<<sum7<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[OCTAL TO DECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long number5;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number5);

                cout<<"\n\n";

                printf("Decimal equivalent is: %d", number5);
                break;

            case 'c':                           //ok
                cout<<endl<<"[OCTAL TO HEXADECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long number6;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number6);

                cout<<"\n\n";

                printf("Hexadecimal equivalent is: %X", number6);
                break;

            default:
                cout<<endl;
        }

        break;

    case 'c':
        cout<<endl<<"[DECIMAL CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Binary"<<endl;
        cout<<"b - Octal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>v;




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

void main(){

    char value;
    char t, u, v;
    char answer;

    do{

    clrscr();

    cout<<"**********NUMBER CONVERSION**********"<<endl;
    cout<<endl;

    cout<<"a - Binary"<<endl;
    cout<<"b - Octal"<<endl;
    cout<<"c - Decimal"<<endl;
    cout<<"d - Hexadecimal"<<endl;
    cout<<endl;

    cout<<"Select a value to be converted: ";
    cin>>value;

    switch(value){

    case 'a':
        cout<<endl<<"[BINARY CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Octal"<<endl;
        cout<<"b - Decimal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>t;

        switch(t){

            case 'a':                           //ok
                cout<<endl<<"[BINARY TO OCTAL CONVERSION]"<<endl;
                cout<<endl;

                long b1,f1=1,d1=0,num5,result5[8]={0,0,0,0,0,0,0,0},flag5=0;
                cout<<"Enter a Binary number: ";
                cin>>b1;

                cout<<"\n\n";

                while(b1>0)
                {
                   if((b1%10)==1)
                   {
                      d1=d1+f1;
                   }
                   b1=b1/10;
                   f1=f1*2;
                }

                num5=d1;
                cout<<"Octal equivalent is: ";
                for(int i5=0;i5<8;++i5)
                {
                   result5[7-i5]=num5%8;
                   num5=num5/8;

                   if(num5==0)
                      break;
                }

                for(i5=0;i5<8;++i5)
                {
                   if(result5[i5]!=0)
                      flag5=1;

                   if(flag5==1)
                      cout<<result5[i5];
                }

                cout<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[BINARY TO DECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long b2,f2=1,d2=0;
                cout<<"Enter a Binary number: ";
                cin>>b2;

                cout<<"\n\n";

                while(b2>0)
                {
                   if((b2%10)==1)
                   {
                      d2=d2+f2;
                   }
                   b2=b2/10;
                   f2=f2*2;
                }

                cout<<"Decimal equivalent is: "<<d2<<endl;
                break;

            case 'c':                           //ok
                cout<<"[BINARY TO HEXADECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long b3,f3=1,d3=0,r6[10],z6=0,number6;
                cout<<"Enter a Binary number: ";
                cin>>b3;

                cout<<"\n\n";

                while(b3>0)
                {
                   if((b3%10)==1)
                   {
                      d3=d3+f3;
                   }
                   b3=b3/10;
                   f3=f3*2;
                }

                number6=d3;
                while(number6>0)
                {
                   r6[z6]=number6%16;
                   number6=number6/16;
                   z6++;
                }

                cout<<"Hexadecimal equivalent is: ";

                for(int j6=z6-1;j6>=0;j6--)
                {
                   if(r6[j6]==10)
                      cout<<"A";
                   else if(r6[j6]==11)
                      cout<<"B";
                   else if(r6[j6]==12)
                      cout<<"C";
                   else if(r6[j6]==13)
                      cout<<"D";
                   else if(r6[j6]==14)
                      cout<<"E";
                   else if(r6[j6]==15)
                      cout<<"F";
                   else
                      cout<<r6[j6];
                }

                cout<<endl;
                break;

            default:
                cout<<endl;
        }

        break;

    case 'b':
        cout<<endl<<"[OCTAL CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Binary"<<endl;
        cout<<"b - Decimal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>u;

        switch(u){

            case 'a':                           //ok
                cout<<endl<<"[OCTAL TO BINARY CONVERSION]"<<endl;
                cout<<endl;

                long number4,dec7,rem7,i7=1,sum7=0;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number4);

                cout<<"\n\n";

                dec7=printf("%d", number4);

                while(dec7>0)
                {
                   rem7=dec7%2;
                   sum7=sum7 + (i7*rem7);
                   dec7=dec7/2;
                   i7=i7*10;
                }

                cout<<"Binary equivalent is: "<<sum7<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[OCTAL TO DECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long number5;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number5);

                cout<<"\n\n";

                printf("Decimal equivalent is: %d", number5);
                break;

            case 'c':                           //ok
                cout<<endl<<"[OCTAL TO HEXADECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long number6;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number6);

                cout<<"\n\n";

                printf("Hexadecimal equivalent is: %X", number6);
                break;

            default:
                cout<<endl;
        }

        break;

    case 'c':
        cout<<endl<<"[DECIMAL CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Binary"<<endl;
        cout<<"b - Octal"<<endl;
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.