Wrap a for loop in a while loop

trying to wrap my for loop in a while loop, getting an error.

Page 1 of 1

1 Replies - 1231 Views - Last Post: 28 October 2008 - 03:29 PM Rate Topic: -----

#1 bbarker3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 12-October 08

Wrap a for loop in a while loop

Posted 28 October 2008 - 02:43 PM

It is the part of the code with while that is messing up. It doesn't like the x. I'm using visual studio 2008 professional. After I build the program, it gives me a warning and no errors(the warning has to do with that "while(x !=0)" I think. So then I Start without Debugging, and an error pops up where I can Abort, Retry, or Ignore. Any ideas on how to fix this? Thanks

#include <stdio.h>
#include <math.h>

int main(void)
{
	int x, n, a;
	
	while (x != 0)   
	 {  
		 printf("please enter a number\n");
		 scanf_s("%d", &x);
		 n = 1;
		 
		 if (x < 0)
		 {
			 printf("value cannot be negative\n");
		 }
		 else
		 {
			 for (a = x; a > 0; a--)
			 {
				 n = n*2;
			 }
			 printf("Two raised to the power of %d is %d\n", x, n);
		 }
	}
return(0);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Wrap a for loop in a while loop

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,558
  • Joined: 23-August 08

Re: Wrap a for loop in a while loop

Posted 28 October 2008 - 03:29 PM

You need to initialize x to something (other than 0) to enter the loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1