Function not returning a value

My binary function isnt returning value, and I'm not understanding why?

Here is the error:
error C2561: 'binary' : function must return a value
see declaration of 'binary'



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int binary(int);
int hex(int);
int octal(int);
int _tmain(int argc, _TCHAR* argv[])
{
    int number,choice;
    cout << "Enter a positive integer: ";
    cin >> number;
   cout<<"What would you like to convert to?"<<endl<<"1-Binary"<<endl<<"2-Hex"<<endl<<"3-Octal"<<endl;
	cin>>choice;
		
	if(choice == 1)
    cout<<binary(number) << endl;
	else if(choice == 2)
    cout<<hex(number) << endl;
	else if(choice == 3)
    cout<<octal(number) << endl;

	return 0;
}
 
int binary(int number) {
    int remainder;
    if(number <= 1) {
        cout << number;
		return;
    }
    remainder = number%2;
    binary(number >> 1);
	cout<<remainder;
	
}

	int hex(int number) {
	long int remainder,quotient;
    int i=1,j,temp;
    char hexadecimalNumber[100];

    quotient = number;
    while(quotient!=0){
         temp = quotient % 16;

      if( temp < 10)
           temp =temp + 48;
      else
         temp = temp + 55;
      hexadecimalNumber[i++]= temp;
      quotient = quotient / 16;
  }
    for(j = i -1 ;j> 0;j--)
	   printf("%c",hexadecimalNumber[j]);
	cout<<endl;
	
	}

	int octal(int number){
	int octalNumber[100],x=1,y;
	long int quotient;

  quotient = number;

  while(quotient!=0){
      octalNumber[x++]= quotient % 8;
      quotient = quotient / 8;
  }
  for(y = x -1 ;y> 0;y--)
  printf("%d",octalNumber[y]);
  cout<<endl;

	}
On line 30 you have a return statement without a value. The return type of binary is int so it has to return an integer. That's why it complains when you try to return from the function without returning a value.

You should also return something when number > 1.
Yea that's what I was thinking, the problem is whenever I try adding "return;" at the end of the functions, it doesnt return a value. And if i try adding "ans=....." into them, and use "return ans;" the answers come out wrong and binary goes through an infinite loop until it crashes.

I've gotten the program to work and give me the conversion to the different bases, but it needs to ask the user which one they want. Thats where i start running into problems because my original functions didnt want to work with the if statements
bumping for more help
once more in hopes of more advice
Topic archived. No new replies allowed.