Convert decimal to base and base to decimal program

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;
}
Im getting an error in the code I have so far.


That's pretty ambiguous.

Build error?

Link error?

Run time error?

Please state what you expect to happen and what actually is happening--something to give us insight into what you think the error is.
PLEASE learn to use code tags, it makes reading and commenting on your code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add the tags.

2 very easily fixed problems, one that isn't.

Problem #1: no closing brace at the end of basetodecimal().

Problem #2: the output statement in decimaltobase() is missing a second <.

Problem #3: calling a nonexistent function, convert10tob() in decimaltobase(). Are you wanting to call decimaltobase() recursively?

Your code using code tags, with a bit of reformatting applied:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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;
   }
My apologies.. This was my first post and didnt realize I made those small dumb errors. I also thought I copy and pasted the error I was getting

error: a function-definition is not allowed here before ‘{’ token
{

Thats the error
Ok i did some rearranging on my program and got it to run however now my functions are not working properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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)
{
     
     int x = num % base;
     while(num != 0)
     {
     num /= base;
     num += 1; 
     decimaltobase(num, base);
     cout<< x << 0 ? x + (base * -1) : x;
    
} 
kboeser wrote:
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]

Topic archived. No new replies allowed.