2

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;
}
5
  • 4
    Now seems like the perfect time to learn how to debug your programs. Commented Jan 14, 2018 at 16:33
  • 1
    You have two cin ... lines; you need only one. Commented Jan 14, 2018 at 16:35
  • Do you really need the extra stuff that std::endl does in all those places? '\n' ends a line. Commented Jan 14, 2018 at 16:36
  • Yeah, I guess I should debug my own program. Sorry for the trouble and thank you :) Commented Jan 14, 2018 at 16:38
  • is this guess my number game? Commented Jan 14, 2018 at 17:04

3 Answers 3

1

and if I enter a number greater than 2000 again it does nothing

That is not right, it is waiting for you to enter the next cin input.Since you have two cins in the loop for a values greater than 2000, the second cin doesn't get a prompt.

Your program can be simplified a lot, change the while loop part like below

while (out){
    cin >> a;
    out=a>2000?1:0;
    if(out)
       cout<<"Enter number again ";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the info !
@blackGreap Check the edit I have just made. Since you have two cins in the loop for values greater than 2000, the second cin doesn't get a prompt.
So that's why it looped forever. Makes sense now. Thank you mate :D
Actually it wouldn't loop for ever. ;-) I am sure the point is clear.
1

I havent tested it, but you could try this:

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main(){

unsigned int a = 0 ;

cout << "Please enter a number : " << endl;

while (true){
    cin >> a;
    if (a > 2000) {
        cout << "Number is greater than 2000 !" << endl;
        cout << endl;
        cout << "Please enter the number again : " << endl;
    } else {
        cout << "Your entered number is : " << endl << a << endl;
        break;
    }
}
return 0;
}

Comments

1

In the if (a > 2000)

do not cin, and your code will start working, the reason is that you don't need to input here because loop will do that for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.