I am trying to take an integer for input and print it only if the number is <= 2000 or else the user is asked to re-enter the number again and again.
When I enter a number greater than 2000 it asks me to enter number again ( which is exactly what I'm trying to do ) and if I enter a number greater than 2000 again it does nothing. It seems that the loop is running forever but I don't know what I did wrong. Any help is appreciated.
Here is the code written in C++.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main(){
unsigned int a = 0 ;
int out = 1;
cout << "Please enter a number : " << endl;
while (out){
cin >> a;
if (a > 2000) {
cout << "Number is greater than 2000 !" << endl;
cout << endl;
cout << "Please enter the number again : " << endl;
cin >> a;
out = 1;
} else {
cout << "Your entered number is : " << endl << a << endl;
out = 0;
}
}
return 0;
}
cin ...lines; you need only one.std::endldoes in all those places?'\n'ends a line.