while loop

Reply

Join Date: Oct 2006
Posts: 1
Reputation: redstar is an unknown quantity at this point 
Solved Threads: 0
redstar redstar is offline Offline
Newbie Poster

while loop

 
0
  #1
Oct 10th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: while loop

 
0
  #2
Oct 10th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 15
Reputation: steveh is an unknown quantity at this point 
Solved Threads: 0
steveh steveh is offline Offline
Newbie Poster

Re: while loop

 
0
  #3
Oct 10th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,831
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: while loop

 
0
  #4
Oct 11th, 2006
  1. while ( x <= 10);
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) )

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

  1. result = result * base;
hmmm, int to long conversion....
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
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