Ok, so i'm working on a program that converts decimal numbers to binary numbers. The binary number is outputted as a string. But the compiler (Turbo C 3.0) won't let me return a string from a function to the main part of the program.
# include <iostream.h>
//validinput function checks to see if the input is greater than 256
//or if it is less than 0
int validinput(int num)
{
if (num > 256 || num < 0)
{
return 0;
}
else
{
return 1;
}
}
//dectobin function converts the decimal number to a binary number
//written with a string
char dectobin(int num)
{
int base2[] = {128, 64, 32, 16, 8, 4, 2, 1};
char binarystring[9];
char *pbin = binarystring;
for(int x = 0; x > 8; x++)
{
if(num > base2[x])
{
pbin[x] = '1';
num =- base2[x];
}
else
{
pbin[x] = '0';
}
}
return binarystring;
}
//main program
int main()
{
char binary[9];
int num;
while(!validinput(num))
{
cout<<"Enter an integer between 0 and 255: ";
cin>> num;
}
binary = dectobin(num);
cout<<"The binary number is: "<< binary;
return 0;
}