use of while loop with flag control varible

this piece of code is running infinty. i am not able to get why is this happening.
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
//flag controlled  while loop
//number guessing game
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
    int num,guess;
    bool isguessed;
    srand(time(0));
    num=rand()%100;
    isguessed=false;
    while(!isguessed)
    {
        cout<<"Enter an integer greater than or equal to 0 and less than 100: ";
        cin>>guess;
        cout<<endl;
    }
    if(guess==num)
    {
        cout<<"you guessed the correct "<<"Number."<<endl;
        isguessed=true;

    }
    else if(guess<num)
    {
        cout<<"your number is lower than the "<<"number.\n  guess again!"<<endl;

    }
        return 0;
}

Last edited on
You don't change isguessed in the while loop that tests it.
1
2
3
4
5
6
while(!isguessed)
{
   cout<<"Enter an integer greater than or equal to 0 and less than 100: ";
   cin>>guess;
   cout<<endl;
}

So that test always returns the same answer and it will loop indefinitely.

Please put your code within code tags. Then we can see the indentation that you (hopefully) are using to clarify code structure.
Last edited on
Please put your code within code tags. Then we can see the indentation that you (hopefully) are using to clarify code structure.
what does this mean, i am new here. Will you please tell me about this.
The forum has a stupid bug that prevents the user from clicking the code tag button in the Original Post.

But you can edit your post and either manually add "[code] [/code]" surrounding your code, or highlight your code and press the [<>] button in the Format section.
Last edited on
like this, i have edited it once.
No, you literally have to put the characters

[code]

#include <whatever>

int main()
{
code code code;
}

[/code]

around your code.

1
2
3
4
5
6
#include <whatever>

int main()
{
    code code code;
}


Or: See the "Format" section to the right-hand side of the posting field?
Highlight your code with your mouse, and then click the button with the <> symbol in it.
Last edited on
thanks.
Topic archived. No new replies allowed.