How to print the multiples of 2?

Pages: 12
I remember this part but I can't just figure it out.. My current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Make a program that keeps printing the multiples of the integer 2. */
#include <iostream>

using namespace std;

int main()
{
    int total;

    while(true)
    {
        cout << " " << total << endl;
        total*2;
    }

    return 0;
}


Don't give me answers, maybe hints will help!

Last edited on
1
2
int total;
total*2; // hint, these may or may not contain errors :) 
Last edited on
Also, avoid infinite loops at all costs. They're very bad programming technique.

Edit: total*2; shouldn't result in an error, but possibly a warning.
Last edited on
Yeah Ik Volatile. I used infinite cuz it told me too.
Your just missing one character
total*2; <-- somewhere in here =D
also, it is a good idea to initialize variables as soon as you make them.
Last edited on
Woah wait a second. are you getting the error:

ld.exe cannot open output file bin\Debug\C++codes.exe Permission denied

?
you are not assigning your value of total * 2 in any thing..
XD oh... I forgot about that.
Bufflez, that happens because you close the terminal before it gets a chance to load the program, the terminal processes is still active, you need to kill it through a task manager, then you re-run it.

I use infinite loops frequently, just breaking out of them when I need to, it's better than goto and uses less memory than if I had some arbitrary bool value for every while loop.
that happens because you close the terminal before it gets a chance to load the program, the terminal processes is still active
This just doesn't happen. Closing the console terminates the program running on it. I believe the program gets a SIGINT to get a chance to quit cleanly.
You're right, it doesn't make sense, but it happens to me all the time using Code::Blocks and that seems to be how I cause it, so what else can I say?
Code::Blocks is a special case because it doesn't run the program directly. You'll notice that at the end it displays its run time and waits for a key press. The regular console doesn't do this. What Code::Blocks does is run a program that itself runs the user's program. Now, what happens to the child process, I have no clue.
closed account (j2NvC542)
Zephilinox, if you 'x' out a running program in Code::Blocks in debug mode, it is still "debugging". To stop the debug process, you need to "abort" it. It's on the line where there is also a button for "compile" and "run" and such.
Last edited on
Aborting usually doesn't work for me, and by debug mode do you mean in contrast with release? or as in actually using a debugger? because I don't use debuggers that often.
closed account (j2NvC542)
In contrast to release.
So, then what do I do to make the program run? (I'm using Code::Blocks)
you can either do what I suggested, and kill it via a terminal or gui application (in Windows 7 press ctrl-shift-esc, click processes tab, find program, end process), bools suggestion of pressing abort (which hasn't worked for me in the past, but may for you) or as a last resort restart your PC.
It is not a simple task as it seems at the first glance. Below is the corresponding code. I restricted the range of values to the type char because for the int type the output will be too big.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <limits>

int main()
{
	for ( char i = 1; i < std::numeric_limits<char>::max(); i++ )
	{
		if ( ( i + 1 & 1 ) == 0 ) std::cout << i + 1 << ' ';
	}

	return ( 0 );
}
Last edited on
closed account (j2NvC542)
To end a program you can also try ctrl+z or ctrl+c.
Pages: 12