請幫我調試下面的代碼。 我想使用函數將二進制數轉換爲十進制或八進制。 我一直在switch語句錯誤「函數調用錯誤太少參數」。C++將二進制轉換爲十進制,八進制
#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];
}
}
除了它不會編譯,所以調試器是毫無意義的。 toDeci()和toOct()需要一個很長的int參數,並且你沒有傳遞任何東西 – Pemdas
在case(0)下,你傳遞給toDeci的參數是多少?它需要多少? –
類似'long int toOct(long int)'是完全無意義的,數字是數字,文本表示是文本表示。 –