Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

function - C++ convert binary to decimal, octal

Please i need help debugging the code below. I am suppose to produce a code using functions that converts binary numbers to decimal or octal. I keep getting error at the switch statement "error too few argument in function call".

#include <iostream.> 

long int menu();
long int toDeci(long int);
long int toOct(long int);

using namespace std;

int main () 
{
int convert=menu();

switch (convert)
{
case(0):
    toDeci();
    break;
case(1):
    toOct();
    break;
    }
return 0;
}
long int menu()
{
int convert;
cout<<"Enter your choice of conversion: "<<endl;
cout<<"0-Binary to Decimal"<<endl;
cout<<"1-Binary to Octal"<<endl;
cin>>convert;
return convert;
}

long int toDeci(long int)
{

long bin, dec=0, rem, num, base =1;

cout<<"Enter the binary number (0s and 1s): ";
cin>> num;
bin = num;

while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout<<"The decimal equivalent of "<< bin<<" = "<<dec<<endl;

return dec;
}

long int toOct(long int)
{
long int binnum, rem, quot;
int octnum[100], i=1, j;
cout<<"Enter the binary number: ";
cin>>binnum;

while(quot!=0)
{
    octnum[i++]=quot%8;
    quot=quot/8;
}

cout<<"Equivalent octal value of "<<binnum<<" :"<<endl;
    for(j=i-1; j>0; j--)
    {
        cout<<octnum[j];
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am suppose to produce a code using functions that converts binary numbers to decimal or octal.

There's no such thing like converting binary numbers to decimal or octal based on numerical representations as

long int toDeci(long int);
long int toOct(long int);

Such functions are completely nonsensical for any semantical interpretation.

Numbers are numbers, and their textual representation can be in decimal, hex, octal or binary format:

dec 42
hex 0x2A
oct 052
bin 101010

are all still the same number in a long int data type.


Using the c++ standard I/O manipulators enable you to make conversions of these formats from their textual representations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...