Multiplying

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;
}
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?
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..
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?
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
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
Topic archived. No new replies allowed.