| BloodMoney (7) | |||||
|
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:
and so on. So far all I get when I run the program is either
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:
Any suggestions? | |||||
|
|
|||||
| iHutch105 (947) | |
|
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. | |
|
|
|
| dcftci (41) | |
| 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 | |
|
|
|
| SamuelAdams (192) | |
|
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. | |
|
|
|
| vlad from moscow (3112) | |
| I think you need a tutor.:) | |
|
|
|