943,947 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1328
  • C RSS
Oct 10th, 2006
0

while loop

Expand Post »
Hi i have this program and have a problem it is a basic program for exponents and now i need to add a while loop so that if the user enters a number greater that 10 it asks them to input a number lower or equal to 10, here is my code can someone help me?.




  1. #include <stdio.h>
  2. main( )
  3. {
  4. /* y */
  5. /* calculate x */
  6. /* */
  7. /* where x and y are integers and y >= 0 */
  8. int base, power;
  9. long result = 1;
  10. int counter = 0;
  11.  
  12. while ( x <= 10);
  13.  
  14.  
  15.  
  16. printf("Enter the base number : ");
  17. scanf("%d",&base);
  18. printf("Enter the nth power to which base will be raised");
  19. scanf("%d",&power);
  20. {
  21. for (counter++ < power)
  22. result = result * base;
  23. }
  24. printf("The base of %d raised to",base the %dth power is %
  25. ld\n",power,result);
  26.  
  27. return 0;
  28.  
  29. }}}
  30.  
Last edited by ~s.o.s~; Oct 11th, 2006 at 1:00 pm. Reason: Added code tags, learn to use them yourself
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
redstar is offline Offline
1 posts
since Oct 2006
Oct 10th, 2006
0

Re: while loop

1. Please edit your post to use code tags to preserve spacing and tabs.


2. main() must be declared like this:
  1. int main()
  2. {
  3. // blabla
  4.  
  5.  
  6. return 0;
  7. }

I realize neither of the above answer your questions, but fix those and we will look at your post again.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 10th, 2006
0

Re: while loop

x is not declared as a variable anywhere that I can see. this should give a complier error.
Also, your while statement is terminated by a semicolon. This would work for do-while statement but not when the using a while by itself. If using a while in this way it should be followed by braces surrounding the code to be executed during each iteration of the loop.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
steveh is offline Offline
15 posts
since Nov 2004
Oct 11th, 2006
0

Re: while loop

Quote ...
  1. while ( x <= 10);
Quote ...
Also, your while statement is terminated by a semicolon
This also has another problem, bacause when x is declared the program will just stay on this line ( while(1) )

Quote ...
  1.  
  2. for (counter++ < power)
I think what you're trying to do is:
  1.  
  2. for (counter = 0; counter < power; counter++)
  3. {
  4. }

Quote ...
  1. result = result * base;
hmmm, int to long conversion....
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

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: removing double quotes using strpbrk()
Next Thread in C Forum Timeline: Read Line from serial port buffer





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


Follow us on Twitter


© 2011 DaniWeb® LLC