HELP!!!!

Reply

Join Date: Sep 2008
Posts: 12
Reputation: msundastud is an unknown quantity at this point 
Solved Threads: 0
msundastud msundastud is offline Offline
Newbie Poster

HELP!!!!

 
0
  #1
Oct 20th, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: HELP!!!!

 
0
  #2
Oct 20th, 2008
Originally Posted by msundastud View Post
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: HELP!!!!

 
0
  #3
Oct 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: HELP!!!!

 
0
  #4
Oct 21st, 2008
>>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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: HELP!!!!

 
0
  #5
Oct 21st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: HELP!!!!

 
0
  #6
Oct 21st, 2008
Originally Posted by stilllearning View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: HELP!!!!

 
0
  #7
Oct 21st, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC