While loop in C++

This is simple while loop in c++ i cant figure out where i am wrong but the the loop is running infinitely what should happen is that after “Wrong user prompt” the it should ask again for user input but that’s not happening where am i wrong at in this code ?

#include <iostream>
using namespace std;


int main()
	{
    bool x = true;

		while(x)
			{
				cout<<"input the name"<<endl;
        int b;
				cin>>b;
				
				if( b == 2)
					{
						cout<<"Right User"<<endl;
						x = false;
					}
				else
					{
						cout<<"Wrong input"<<endl;
					}		
				
			}
	}

That’s working properly for me. You may want to use a debugger to figure this out.

you should set the x to false also in else block just like what you did in if block, because without that, it cannot exit the while loop so it will be an indefinite loop

I was also stuck in the loop when I accidently entered my name in the input.
The program is accepting an integer value whereas it gets an character resulting in an endless loop.
There is no problem in the code its a problem with the input.

I was mistaken by this line and entered my name accidently instead of integer value.
Online C++ Compiler - Google Chrome 2021-07-03 11-47-47

Even if the problem is not resolved

you can use this code instead

#include <iostream>
using namespace std;


int main()
	{
    bool x = false;
	while (x == false){
	    int b;
	    cout << "input the value" << endl;
	    cin >> b;
	    if(b == 2){
	        cout << "Right User" << endl;
	        x = true;
	    }else {
	        cout << "Wrong Input" << endl;
	    }
	}
	cout << "Terminating" << endl;
 	return 0;
	}

OR

If you dont want to use my code :arrow_up: :arrow_up: just change the condition in the while loop of your code

set it to

while(x == true)

:spiral_notepad:NOTE: Never enter a char or any other character type or a floating point value in and integer input and vice versa. It causes memory corruption and an infinite while loop as you might have experienced.