| | |
HELP!!!!
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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
C Syntax (Toggle Plain Text)
#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"); } } }
>>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.
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.
Last edited by devnar; Oct 21st, 2008 at 1:31 pm.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
A void function would generally look like this
But trying to do this is wrong.
Moral, you cannot return values from a void function. Its incorrect, and a good compiler should throw an error if you attempt it.
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.
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.
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.
![]() |
Other Threads in the C Forum
- Previous Thread: A C programming under Linux
- Next Thread: itoa function (or similar) in Linux
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h





