Can someone help me with this code?

b should be getting one added to it every 20 ticks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 #include <iostream>
using namespace std;



int main()
{
    int x;
    int b;
    int i;


    while(i<=40){
        if(x!=20){
            ++x;
        }
        else{
            ++b;
            x = 0;
        }
    };
    while(i<=40){
        cout << "Bacon count: " << b << endl;
    }
    return 0;
}

i never reaches 40 and the cout is out of the main while loop.
Here is a fix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;



int main()
{
    int x = 0;
    int b = 0;
    int i = 0;


    while(i<40)
    {

        if(x!=20)
        {
            ++x;
        }
        else if (x == 20)
        {
            ++b;
            x = 0;
            ++i;
        }
        cout << "Bacon count: " << b << endl;
    }
    return 0;
}
Topic archived. No new replies allowed.