Multiplying

May 23, 2011 at 3:19am
At the end of the program, crash never gets mutiplied and shows up as 0 in the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int crash, no;
    crash = 22;
    no = 5;
    cout<<"start C:\Program Files\Internet Explorer\iexplore.exe\n";
    while (no = 5)
    {
          cout<<"TRY #" <<crash<< " 5\n";
          crash = 2 * crash;
          }
    system("PAUSE");
    return EXIT_SUCCESS;
}
May 23, 2011 at 3:57am
You are saying "at the end of the program", but I have trouble believing that program would ever terminate; it should get stuck in an infinite loop. I believe line 12 is an error, but it's hard to tell (Do you want to use the comparison operator, == ?). Even if you did, it should still infinite loop, because you never modify no inside the while loop.

What do you want this program to do?
May 23, 2011 at 6:55am
The program will jump into infinite loop due to the while statement at line 12. and the reason u see the value of crash shows up as 0 is because the program keep multiplied the crash in the infinite loop and the value of crash become bigger and bigger and finally become 0. (to represent infinity i think)..
So, the problem arise at while statement at line 12..
May 23, 2011 at 2:34pm
Im trying to create an infinite loop, but how to I make it so that the number shows up multiplied by 2 every time? I cant?
May 23, 2011 at 3:02pm
On line 12, you're using assignment, instead of comparison.

assignment: no = 5
comparison: no == 5

Edit:
You could also do:
1
2
3
4
5
while( true )
{
    //code...
    break; //use break to exit an infinite loop
}
Last edited on May 23, 2011 at 3:03pm
May 23, 2011 at 3:24pm
closed account (z05DSL3A)
If you keep multiplying an integer by two (effectively bit shifting left) you will eventually end up with no bits set in your integer (ie zero), from there on you will only get zero out.

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

using namespace std;

int main(int argc, char *argv[])
{
    int number = 1;

    for(int i = 1; i < 35; ++i)
    {
        cout << i << "\t" << number << endl;
        number *=2;
    }
    return EXIT_SUCCESS;
}

Last edited on May 23, 2011 at 3:33pm
Topic archived. No new replies allowed.