How to print the multiples of 2?

Pages: 12
Aug 7, 2012 at 3:36am
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 Aug 7, 2012 at 3:36am
Aug 7, 2012 at 3:39am
1
2
int total;
total*2; // hint, these may or may not contain errors :) 
Last edited on Aug 7, 2012 at 3:40am
Aug 7, 2012 at 3:40am
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 Aug 7, 2012 at 3:41am
Aug 7, 2012 at 3:41am
Yeah Ik Volatile. I used infinite cuz it told me too.
Aug 7, 2012 at 4:56am
Your just missing one character
total*2; <-- somewhere in here =D
Aug 7, 2012 at 5:01am
also, it is a good idea to initialize variables as soon as you make them.
Last edited on Aug 7, 2012 at 5:01am
Aug 7, 2012 at 5:05am
Woah wait a second. are you getting the error:

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

?
Aug 7, 2012 at 6:13am
you are not assigning your value of total * 2 in any thing..
Aug 7, 2012 at 7:06am
XD oh... I forgot about that.
Aug 7, 2012 at 8:09am
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.
Aug 7, 2012 at 8:18am
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.
Aug 7, 2012 at 8:25am
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?
Aug 7, 2012 at 8:58am
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.
Aug 7, 2012 at 11:43am
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 Aug 7, 2012 at 11:43am
Aug 7, 2012 at 11:51am
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.
Aug 7, 2012 at 12:04pm
closed account (j2NvC542)
In contrast to release.
Aug 7, 2012 at 12:58pm
So, then what do I do to make the program run? (I'm using Code::Blocks)
Aug 7, 2012 at 3:44pm
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.
Aug 7, 2012 at 4:16pm
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 Aug 7, 2012 at 4:17pm
Aug 7, 2012 at 4:41pm
closed account (j2NvC542)
To end a program you can also try ctrl+z or ctrl+c.
Pages: 12