OK so I'm almost done with my program. How to put this in function like putting each case like void Binary2Decimal() , void Octal2Decimal() & , void Hexa2Decimal() 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;
}

Recommended Answers

All 13 Replies

First create the empty functions what you need.
Next copy the code from the swith statement into the functions
Finally, replace the code in the swith statement with function calls.

How do you call a function? Very simply like this (which you have already posted):

Binary2Decimal();

commented: Can you make an example. Use my program us an example. +1

Can you show me? How about my program make an example of it. In Turbo C++

I don't know how. I get an decleration terminated incorrectly in d{ . When I put a call function.

Help please :(. Just use my program to put a call function. I've tried it and I always get decleration terminated incorrectly in d{.

Can someone help me about this? Just edit my program to put a call function like void Binary2Decimal() , void Octal2Decimal() & , void Hexa2Decimal() in "TURBO C++"? Please2x :((((((((

It's very similar to how you coded main()

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


void Binary2Decimal() 
{

}

int main()
{


}

I tried my best to put function call on this program but I failed. Can you can just correct this. NOTE this is TURBO C++

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

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

void Binary2Decimal()
{
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;

}

void Octal2Decimal()
{

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;

}

void Hexa2Decimal()
{
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;

}

int 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;

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

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

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

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

You don't need the switch statement inside the function, just inside main().
Delete lines 142-150 because that code is now in each of the functions that you created.

    void Binary2Decimal()
    {
        cout<<endl<<"[BINARY TO DECIMAL CONVERSION]"<<endl;
        cout<<endl;
        long b2,f2=1,d2=0;
        cout<<"Enter a Binary number: ";
        cout<<"nn";
        while(b2>0)
        {
           if((b2%10)==1)
           {
             d2=d2+f2;
           }
           b2=b2/10;
           f2=f2*2;
        }
        cout<<"Decimal equivalent is: "<<d2<<endl;
   }


    int 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':
        Binary2Decimal();
        break;
    case 'b':
        Octal2Decimal();
        beak;
    case 'c':
        Hexa2Decimal();
        break;
    default:
        cout<<"Wrong input" << endl;
    }
    cout<<"Do you want to continue NUMBER CONVERSION?(y/n) ";
    cin>>answer;
    }
    while(answer == 'y');
    cout<<endl;
    }

Can I do this the same as Octal2Decmimal and Hexa2Decimal?

Ok so. I've been using the wrong function. My prof said that she will not accept my program because she said that cout,cin & etc... are not allowed use only the older style.. So I try to edit my program. Can you tell what's the problem here? Cout is so much easier to use. I wish cout & printf are the same :(

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

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

void Binary2Decimal()
{
printf("[BINARY TO DECIMAL CONVERSION]");

int b2,f2=1,d2=0;
printf("Enter a Binary number: ");
printf("\n");

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

void Octal2Decimal()
{
printf("[OCTAL TO DECIMAL CONVERSION]");

int number4,dec7,rem,i7,sum7;
printf("Enter an Octal number: ");
scanf("%o", &number4);

printf("\n");

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

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

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

void Hexa2Decimal();

printf("[HEXADECIMAL TO DECIMAL CONVERSION]");

int number1,dec8,rem8,i8,sum8;
printf("Enter a Hexadecimal number");
scanf("%X", &number1);

printf("\n");

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

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

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

}

int main()
{
do{
clrscr();
printf("Conversion from any base to base 10");

gotoxy(1,3);printf("a - Binary");
gotoxy(1,4);printf("b - Octal");
gotoxy(1,5);printf("c - Hexadecimal");

gotoxy(1,8);printf("Select a value to be converted: ", &value);

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

default:
printf("Wrong input");
}
printf("Do you want to continue NUMBER CONVERSION?(y/n)", &answer);
}
while(answer == 'y');
}

This is really wrong :( Help please. There are so many wrong in this program. I just edited this to printf style but there are so many wrong in this program. This program will run but in doesn't running right :((

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


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

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


char b2,f2=1,d2=0;
gotoxy(1,19);printf("Enter a Binary number: ");
scanf("%d", &b2);

printf("\n\n");

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

}

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


char number4,dec7,rem7,i7,sum7;
gotoxy(1,19);printf("Enter an Octal number: ");
scanf("%o", &number4);

printf("\n\n");

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

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

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

}

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


char number4,dec7,rem7,i7,sum7;
gotoxy(1,19);printf("Enter an Octal number: ");
scanf("%o", &number4);

printf("\n\n");

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

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

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

}
int main()

{

do{
clrscr();

printf("Conversion from any base to base 10");


gotoxy(1,3);printf("a - Binary");
gotoxy(1,4);printf("b - Octal");
gotoxy(1,5);printf("c - Hexadecimal");


gotoxy(1,7);printf("Select a value to be converted: ");
scanf("%c", &value);

switch(value){
case 'a':
Binary2Decimal();
break;
case 'b':
Octal2Decimal();
break;
case 'c':
Hexa2Decimal();
break;
default:
printf("Wrong input");
}

gotoxy(1,22);printf("Do you want to continue NUMBER CONVERSION?(y/n) ");
scanf("%c", &answer);
}

while(answer == 'y');
getch();
}

My prof said that she will not accept my program because she said that cout,cin & etc... are not allowed use only the older style..

Hold on for a moment. Is this a course in C++, or in C? Most C++ compilers (including Turbo C++) will also compile C programs. If the course is supposed to be in C, it would explain why the professor only wants the C-style libraries.

Otherwise, I would say you professor is an incompetent and you should find a better instructor, or perhaps a better university. Of course, I also feel that way about choosing Turbo C++ as the compiler in the first place, but I know that some countries have standardized on it for all programming courses (though as far as I am aware the Philippines is not one of them), so there may not be much the professor can do about that.

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.