can't figure out what I'm doing wrong....Im trying to write a program that can convert a sequence of positive integers from base-10 (decimal) to base-2 (binary), but I can't use the pow function

#include <stdio.h>
#include <stdlib.h>

void input_check (int start, int stop)
{
     if (( 0 <= start) && (start <= stop) && (stop <= 255))
         
         return;
         
         else
             exit(1);
}

int powerup (int m, int h)
{
    int z = m;
    int i = 0;
    
    if (h == 0)
       return 1;
    for (i = 1; i < h; i++)
    {
        z = z * m;
    }
    
    return z;
}


void convert (int y)
{
    int i;
    int check, conver;
    for (i = 7; i >= 0; i--)
    { 
        check = conver(2, i)
        if (y >= check)
        {
              printf("1");
              y = y - check;
        }
        else
            printf("0");
        }
     return;         
}


int main(void)
{
    int start = 0; 
    int stop = 0;
    int t = 0;


printf(" Please enter a start number:  ");
scanf(%d, &start);
printf(" Please enter a stop number:  ");
scanf(%d, &stop);

{
         input_check (start, stop);
         
         for(t = start; t <= stop; t++)
         {
               printf("%d ", t);
               convert ( t);
               printf("\n");
         }          


}
}

Recommended Answers

All 6 Replies

can't figure out what I'm doing wrong....

A lot of things!
First, recheck your syntax, there are many errors.
And you cannot declare void function and then expect to get some result from it!

a return statement must be accompanied by what a function returns. Void functions dont return values and you need to indent your code as well as using braces

>>And you cannot declare void function and then expect to get some result from it

Is it a bad practice or against the standard? Cause even i use it in functions when I want to pass control back to the main function.

I know the convert function in this program isn't doing anything since it isn't returning any value and since t isn't passed by reference, but I'm asking if it's generally wrong to just have a return statement in a void returning function.

A void function would generally look like this

void foo(){
  return;
}

But trying to do this is wrong.

void foo(){
  int retvalue;

  return retvalue;
};

Moral, you cannot return values from a void function. Its incorrect, and a good compiler should throw an error if you attempt it.

A void function would generally look like this

void foo(){
  return;
}

But trying to do this is wrong.

void foo(){
  int retvalue;

  return retvalue;
};

Moral, you cannot return values from a void function. Its incorrect, and a good compiler should throw an error if you attempt it.

Yeah, that i know. But reading joshmo's and sci@phy's quotes i thought that there might be some mistake in using return in a void function. Maybe i just misinterpreted them.

As for the program, there are many mistakes:

1. convert function isn't doing anything since it is taking argument by value and not returning anything. You should either use a pointer or return back a value.

2. There's an integer variable declared as conver which is later being used as a function. (I'm assuming it's powerup function)

3. And I don't see how your program would work even after successful compilation. If you wanna convert an integer from decimal to binary, all you need is a simple modulus operation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.