943,923 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 625
  • C RSS
Oct 20th, 2008
0

HELP!!!!

Expand Post »
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

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void input_check (int start, int stop)
  6. {
  7. if (( 0 <= start) && (start <= stop) && (stop <= 255))
  8.  
  9. return;
  10.  
  11. else
  12. exit(1);
  13. }
  14.  
  15. int powerup (int m, int h)
  16. {
  17. int z = m;
  18. int i = 0;
  19.  
  20. if (h == 0)
  21. return 1;
  22. for (i = 1; i < h; i++)
  23. {
  24. z = z * m;
  25. }
  26.  
  27. return z;
  28. }
  29.  
  30.  
  31. void convert (int y)
  32. {
  33. int i;
  34. int check, conver;
  35. for (i = 7; i >= 0; i--)
  36. {
  37. check = conver(2, i)
  38. if (y >= check)
  39. {
  40. printf("1");
  41. y = y - check;
  42. }
  43. else
  44. printf("0");
  45. }
  46. return;
  47. }
  48.  
  49.  
  50. int main(void)
  51. {
  52. int start = 0;
  53. int stop = 0;
  54. int t = 0;
  55.  
  56.  
  57. printf(" Please enter a start number: ");
  58. scanf(%d, &start);
  59. printf(" Please enter a stop number: ");
  60. scanf(%d, &stop);
  61.  
  62. {
  63. input_check (start, stop);
  64.  
  65. for(t = start; t <= stop; t++)
  66. {
  67. printf("%d ", t);
  68. convert ( t);
  69. printf("\n");
  70. }
  71.  
  72.  
  73. }
  74. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msundastud is offline Offline
12 posts
since Sep 2008
Oct 20th, 2008
0

Re: HELP!!!!

Click to Expand / Collapse  Quote originally posted by msundastud ...
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!
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 21st, 2008
0

Re: HELP!!!!

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
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Oct 21st, 2008
0

Re: HELP!!!!

>>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.
Last edited by devnar; Oct 21st, 2008 at 1:31 pm.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Oct 21st, 2008
0

Re: HELP!!!!

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 21st, 2008
0

Re: HELP!!!!

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.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Oct 21st, 2008
0

Re: HELP!!!!

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.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: A C programming under Linux
Next Thread in C Forum Timeline: itoa function (or similar) in Linux





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC