### Help - Program that displays a generated number (1,2,4,8,16,32,etc)

Hi, I'm working on a program that is supposed to use a for loop to generate a number that runs in an increment to display numbers like 1,2,4,8,16,32,64...basically to double the number starting from 1.

Here is how my output should look:
 ```1 2 4 8 16 32 64 128 ```

and so on.

So far all I get when I run the program is either
 ```1 Press any key to continue...```

or it shows a one, but sits there for several seconds and does nothing then says "Press any key to continue".

Here's the code:
 ``1234567891011121314151617181920`` ``````/* The increment expression in a for loop need not always alter the loop control variable by a fixed amount. Instead, the loop control variable can change in any arbitrary way. Using this concept, write a program that uses a for loop to generate and display the progression 1, 2, 4, 8, 16, 32, and so on. */ #include using namespace std; int main() { int i = 1; cout << i << " " << endl; for(i > 0; i*2; i++) return 0; }``````

Any suggestions?
Yep, your for loop needs tweaking.

Remember, the syntax for a for loop is for( initialisation; condition; increase ).

You want it to start from 1? Initialise is i = 1.
You want it to go up to 128? Condition is i <= 128.
You want it to double? Increase is i *= 2.
and you should include the cout function in for loop too. so that each value can be seen on the screen as the loop works
While your at it, make sure your number is larger than 0 which will do nothing when doubled and smaller than (half the max size , less i), else you run into some other issues.

put your cout after the for statement
remove << " " unless writing to a file, it has no purpose on screen if you use endl.
I think you need a tutor.:)
Topic archived. No new replies allowed.