Not compile

the code wont compile ,, whats wrong with this code ?!!
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
31
32
  #include <iostream>

int main(){
    
    int a, b, c = 0, d;
    int i = 0;
    int j = 0;
    int l = 0;
    std::cout << "Enter a number";
    std::cin >> a;
    if ( a >= 365 ){
        do{
            b = a - 365;
            i++;
        }while(b < 365);
    }
    if ( b >= 30 ){
        do{
            c = b - 30;
            j++;
        }while ( c < 30 );
    }
    if ( c >= 1 ){
        do{
            d = c - 1;
            l++;
        }while (d != 0 );
    }
    std::cout <<"Answer is: " << i << std::endl << j << std::endl << l;
    
    return 0;
}
1.) You never initialized b.

2.) Your first do while loop is infinite if the program reaches the loop as you are never changing b or a in the loop. Same thing with all of your loops actually. If your program reaches any of the loops it will be stuck in an infinite loop.
Thanks For your response ..

1 ) i am used b in the second loop and as its int so it was = 0

2 ) you mean i need to put an stop condition for each loop ?!!
What you are using in each of your if statements are conditional loops. They check the condition that you have specified earlier.

The problem with your conditions is that if they are processed to true, they will never leave the loop. For example your first if statement and subsequent loop.

You start by checking if a is greater than or equal to 365. Let's say that a is 100. Well now I set be to equal a (100) - 365. Well b is now equal to -265. Okay no problem yet. You increment your i++. Now! We reach your condition for your do-while loop. It checks if b is less than 365. Well hey! b = -265 so yes this is a true statement b is in fact less than 365 so let's start the loop again.

But wait what's this??!? No where in the loop is b updated/incremented/anything. 'b' will ALWAYS be the same. No matter what now, the loop will continue on forever until you stop the program or you reach an error. This is the case for every single one of your loops. When you create a loop you need to make sure there is a way out. Right now, how all of your loops are designed if the program ever ends up in one of them. It's stuck and will never leave.
Topic archived. No new replies allowed.