Ok so I manage to built the program I want but there are problems. #1 The conversion from octal to decimal is not correct. #2 The conversion from hexadecimal to decimal some answers are correct. #3 I don't know how to character array this program. So what I am asking is to help me correct my #1 & 2 problems and my #3 problem help me how to put this in character array. My defense is tomorrow and I'm really dead if I can't put this program in to correct that what my professor is asking. Here's the source code note this is in Turbo C++.

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

void main()
{
char value;
char t, u, v;
char answer;

do{
clrscr();

cout<<"Conversion from any base to base 10"<<endl;
cout<<endl;

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

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

switch(value){
case 'a':
cout<<"[BINARY CONVERSION]"<<endl;
cout<<endl;

cout<<"a - Decimal"<<endl;
cout<<endl;

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

switch(t){
case 'a':                                            //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;

default:
cout<<endl;
}
break;

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

cout<<"a - Decimal"<<endl;
cout<<endl;

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

switch(u){
case 'a':                                      //ok
cout<<endl<<"[OCTAL TO DECIMAL 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<<"Decimal equivalent is: "<<sum7<<endl;
break;

default:
cout<<endl;
}
break;

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

cout<<"a - Decimal"<<endl;
cout<<endl;

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

switch(v){
case 'a':                                     //ok
cout<<endl<<"[HEXADECIMAL TO DECIMAL CONVERSION]"<<endl;
cout<<endl;

long number3;
cout<<"Enter a Hexadecimal number: ";
scanf("%X", &number3);

cout<<"\n\n";
printf("Decimal equivalent is: %d", number3);
break;

default:
cout<<endl;
}
break;

default:
cout<<endl;
}
cout<<endl;

cout<<"Do you want to continue NUMBER CONVERSION?(y/n) ";
cin>>answer;
}

while(answer == 'y');
cout<<endl;
}

While it is unfortunate that it took so long for one of us to get back to you, you have to understand that this can and often does happen on these fora; we are doing this on a volunteer basis, and there isn't always someone able or willing to answer a given question on the forum. That having been said, I'll address several issues with your code:

  • First off, you are using the older style of headers; the standard C++ headers (since 1998) omit the '.h' extensions, and the C library headers are pre-pended with a 'c'. Thus, you would need to use <iostream> and <cstdio> when using a modern compiler.
  • The header <conio.h> is not a standard header, and while some compilers have a version of it, you should aviod it. It was originally specific to a particular MS-DOS compiler from 1989 (Turbo C), and has never been part of the language. DO NOT USE IT IN NEW PROGRAMS, PERIOD.
  • The return type of main() is int. It always has been, and should never be anything else. void main() is simply wrong, and most compilers will warn you about using that.
  • Your code is not formatted; you should always apply some kind of indentation to your code. If the code isn't formatted in some manner, most experienced programmers won't even try to work with it. This may well be the reason you didn't get any responses.
  • You mix C and C++ stream I/O functions, which is allowable but poor style. Try to stick to using one or the other, not both.

Most importantly, you demonstrate a serious conceptual misunderstanding of the nature of the project. If you read in the input as integer values (e.g., long), the input functions will automatically convert the input strings to longs for you, with the default being that they are to be interpreted as decimal values. There are selectors which can allow you to read in values as binary, octal or hex values, but using them would defeat the point of the exercise.

What you need to do to is read the values in as strings, and then convert the string representations to a long binary representation, and vice versa.

Fortunately, you only actually need a single function to do the actual conversion, so long as it takes the base to convert from as a parameter. You will want another function to convert the value to the decimal representation, as well, though.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

long string_to_long(string representation, unsigned int base);
string long_to_string(long value, unsigned int base);

int main()
{
    unsigned int base;
    long converted;
    string value, decimal;
    char answer;

    do
    {
        cout << "Enter a value to be converted: ";
        cin >> value;

        cout << "Enter the base to convert from (1-36):";
        cin >> base;

        converted = string_to_long(value, base);
        decimal = long_to_string(converted, 10);

        cout << value << " in base " << base
             << " becomes decimal " << decimal
             << endl << endl;

        cout << "Do you want to continue NUMBER CONVERSION?(y/n) ";
        cin >> answer;
    }

    while(toupper(answer) == 'Y');
    cout << endl;
}

I'll leave writing the functions in question to you; the page linked to by ddanbe should help.

Edited 2 Years Ago by Schol-R-LEA

Hey thank you very much. I don't know any modern C++ compiler. Can you suggest some? In our school we are using Turbo C++.

My Im using turbo C++ the header #include<iostream> isn't working in my compiler. Can you suggest to me a modern Turbo C++.

Edited 2 Years Ago by john.kane.100483

Oh, I see. That puts you in a rather sticky position, as the instructor doubtless is expecting the program to work with the Turbo compiler. I normally would recommend Code::Blocks with the MinGW GCC compiler, and still would suggest that you get it for your own use, but for classwork, it sounds like you're stuck with TC++.

(BTW, there is a compiler which is the lineal descendant of Turbo, sort of, namely Embarcadero C++ Builder XE5. However, it is commercial software - though there is a free demo, you have to pay to use it past the trial period - and is so radically different from Turbo C++ that they are for all intents and purposes unrealted compilers.)

The Turbo C++ compiler dates from 1989, which is nine years before the new headers were introduced, so no, it won't work with those. It also won't run on modern versions of Windows (i.e., Vista and later) without a DOS emulator such as DosBox. Turbo C++ is a dead-end technology, but at this stage I'm not sure what you can do about it if the coursework requires it.

OK so I'm almost done with my program. How to put this in function like putting each case like void Screen_Header() , void Data_Entry() & , void Display() in "TURBO C++"? Call a function like I only want to display only the Binary to Decimal case.

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

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


void main()

{

do{
clrscr();

cout<<"Conversion from any base to base 10"<<endl;
cout<<endl;

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

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

switch(value){
case 'a':
cout<<"[BINARY CONVERSION]"<<endl;
cout<<endl;

cout<<"a - Decimal"<<endl;
cout<<endl;


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

switch(t){
case 'a':                                            //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;

default:
cout<<endl;
}
break;

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

cout<<"a - Decimal"<<endl;
cout<<endl;

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

switch(u){
case 'a':                                      //ok
cout<<endl<<"[OCTAL TO DECIMAL CONVERSION]"<<endl;
cout<<endl;

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

cout<<"\n\n";

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

{
rem7=dec7%8;
sum7=sum7 + (i7*rem7);
}

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

default:
cout<<endl;
}
break;

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

cout<<"a - Decimal"<<endl;
cout<<endl;

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

switch(v){
case 'a':                                     //ok
cout<<endl<<"[HEXADECIMAL TO DECIMAL CONVERSION]"<<endl;
cout<<endl;

long number1,dec8,rem8,i8=1,sum8=0;
cout<<"Enter a Hexadecimal number: ";
scanf("%X", &number1);

cout<<"\n\n";

dec8=printf("%d", number1);

{
rem8=dec8%16;
sum8=sum8 + (i8*rem8);
}

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

default:
cout<<endl;
}
break;

default:
cout<<endl;
}
cout<<endl;

cout<<"Do you want to continue NUMBER CONVERSION?(y/n) ";
cin>>answer;
}

while(answer == 'y');
cout<<endl;
}

Edited 2 Years Ago by john.kane.100483

Please go back and read what I had to say about int main() and proper formatting. I know it may seem trivial, but these are important, even in Turbo C++. Maybe especially in Turbo C++.

As for writing a function, well, the good news it, you already know how to do that. The main() function may be privileged, but it is still basically a function (mostly) like any other. If you want to add a function of your own, there are basically three things to do:

  • Figure out what the function is supposed to do, and what parameters (inputs from the function calling it) it will need, and what kind of value it will return. Let's start with the menu() function; all it has to do is print out the menu, so it doesn't take any parameters and returns no value.

    void menu()

This is the function's delcaration, or its head, so to speak; it says what the function's name is, what its parameters are (none) and what its return type is (void, or no return value). The next thing after the function declaration comes the implementation or body: in this case, it's just the code to print the menu. Together, they look like this:

void menu()
{
    clrscr();

    cout<<"Conversion from any base to base 10"<<endl;
    cout<<endl;

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

Now, before you can use the function, you need to tell the function that is going to call it (main(), in this case) that the function exists and what its parameters and type are. To do this, you create a function prototype and add it before the function that uses it. The prototype is just a copy of the declaration, with a semi-colon at the end. All it does is makes the compiler aware of what to expect, so it can check the types and number of arguments and the return value.

void menu();

Now we're ready to call the function:

int main()
{
    do 
    {
        menu();
        // and the rest of the program follows

If you look at the code I posted earlier, you'll see a pair of function prototypes for functions with parameters and return values.

long string_to_long(string representation, unsigned int base);
string long_to_string(long value, unsigned int base);

The delarations and implementation for one of them would look like this:

long string_to_long(string representation, unsigned int base)
{
    long result;

    // some code here

    return result;
}

Now, you might ask how you use a function that has a parameter and a return; to which I say, you already have, more than once. Functions like printf() and scanf() are not part of the language, per se; they are library functions, and in principle no different from those you write yourself. Even the << and >> operators which go with cout and cin are actually library functions, even though they seem so different form the others (most of the other operators are really built-in, though).

A typical function call would look like this:

    converted = string_to_long(value, base);

where value and base are the arguments (the values which get copied into the parameters), and converted gets the return value of the function.

This article has been dead for over six months. Start a new discussion instead.