I need to create a program that converts a number from any base to decimal and vice versa. Im getting an error in the code I have so far. Any help would be greatly appreciated. Thank you.
Here is my code I have so far.
#include <iostream>
using namespace std;
int basetodecimal(int num, int base);
int decimaltobase(int num, int base);
int main ()
{
int num, base, choice;
cout<<"Please choose: "<<endl;
cout<<"1. Base to Decimal"<<endl;
cout<<"2. Decimal to Base"<<endl;
cout<<"3. Quit Program"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Please enter your number and base: ";
cin>>num;
cin>>base;
cout<<"The answer is "<<basetodecimal(num, base);
break;
case 2:
cout<<"Please enter your number and base: ";
cin>>num;
cin>>base;
cout<<"This answer is "<<decimaltobase(num, base);
break;
case 3:
cout<<"Good bye";
return 0;
}
}
int basetodecimal(int num, int base)
{
int multiplier=1, result=0;
while(num>0)
{
result=result+num%10*multiplier;
multiplier=multiplier*base;
num=num/10;
}
int decimaltobase(int num, int base)
{
if (num == 0)
return;
int x = num % base;
num /= base;
if (x < 0)
num += 1;
convert10tob(num, base);
cout<< x < 0 ? x + (base * -1) : x;
return;
}
#include <iostream>
usingnamespace std;
int basetodecimal(int num, int base);
int decimaltobase(int num, int base);
int main()
{
int num, base, choice;
cout << "Please choose: " << endl;
cout << "1. Base to Decimal" << endl;
cout << "2. Decimal to Base" << endl;
cout << "3. Quit Program" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Please enter your number and base: ";
cin >> num;
cin >> base;
cout << "The answer is " << basetodecimal(num, base);
break;
case 2:
cout << "Please enter your number and base: ";
cin >> num;
cin >> base;
cout << "This answer is " << decimaltobase(num, base);
break;
case 3:
cout << "Good bye";
return 0;
}
}
int basetodecimal(int num, int base)
{
int multiplier = 1, result = 0;
while (num > 0)
{
result = result + num % 10 * multiplier;
multiplier = multiplier * base;
num = num / 10;
}
int decimaltobase(int num, int base)
{
if (num == 0)
return;
int x = num % base;
num /= base;
if (x < 0)
num += 1;
convert10tob(num, base);
cout << x < 0 ? x + (base * -1) : x;
return;
}
#include <iostream>
usingnamespace std;
int basetodecimal(int num, int base);
int decimaltobase(int num, int base);
int main ()
{
int num, base, choice;
cout<<"Please choose: "<<endl;
cout<<"1. Base to Decimal"<<endl;
cout<<"2. Decimal to Base"<<endl;
cout<<"3. Quit Program"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Please enter your number and base: ";
cin>>num;
cin>>base;
cout<<"The answer is "<<basetodecimal(num, base);
break;
case 2:
cout<<"Please enter your number and base: ";
cin>>num;
cin>>base;
cout<<"This answer is "<<decimaltobase(num, base);
break;
case 3:
cout<<"Good bye";
return 0;
}
}
int basetodecimal(int num, int base)
{
int multiplier=1, result=0;
while(num>0)
{
result=result+num%10*multiplier;
multiplier=multiplier*base;
num=num/10;
}
}
int decimaltobase(int num, int base)
{
int x = num % base;
while(num != 0)
{
num /= base;
num += 1;
decimaltobase(num, base);
cout<< x << 0 ? x + (base * -1) : x;
}
Ok i did some rearranging on my program and got it to run
Did you now?
Well I just tried it in cpp.sh and got the following errors. Please have a go at fixing them> they give the line numbers and they are fairly self-explanatory.
In function 'int basetodecimal(int, int)':
44:1: warning: no return statement in function returning non-void [-Wreturn-type]
In function 'int decimaltobase(int, int)':
55:24: warning: second operand of conditional expression has no effect [-Wunused-value]
55:41: warning: third operand of conditional expression has no effect [-Wunused-value]
57:1: error: expected '}' at end of input
57:1: warning: no return statement in function returning non-void [-Wreturn-type]