Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6222485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:19:20+00:00 2026-05-24T08:19:20+00:00

bool choose() { int answer = 0; while(answer != 1 || answer != 2)

  • 0
bool choose() {
    int answer = 0;
    while(answer != 1 || answer != 2) {
        cout << endl << "Do you want to encrypt(enter 1) or decrypt(enter 2)?" << endl;
        cin >> answer;
    }
    if(answer == 1) return true;
    return false;
}

What is the best way to read from line and compare input with integers?
I know ways cin , gets , getline(cin, answer) .
Which should I use and why?

At the moment, this way is not working, because when i enter 1 or 2, it still stays in while.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:19:21+00:00Added an answer on May 24, 2026 at 8:19 am

    This code is a very good candidate for do-while loop, and if you use that you’re not required to initialize the variable answer.

    int answer; //= 0; no need to initialize!
    do {
    
      cout<<"Do you want to encrypt(enter 1) or decrypt(enter 2)?"<<endl;
      cin >> answer;
    
    }while(answer != 1 && answer != 2);
    

    And of course, you need to use && as others has already pointed out. 🙂

    By the way, what would happen if user entered any non-integer input such as hgjkhg? cin>>answer would fail to read the input and it will remain there forever, and the flag of cin will be set failure, and cin will not be able to read futher input. That means, the loop will never exit!

    To avoid this, and to make the code more robust and complete you should write it as:

    int answer = 0; 
    while(answer != 1 && answer != 2) {
    
      cout<<"Do you want to encrypt(enter 1) or decrypt(enter 2)?"<<endl;
      if ( !(cin >> answer) )
      {
        cin.clear(); //clear the failure flag if there is an error when reading!
        std::string garbage;
        std::getline(cin, garbage); //read the garbage from the stream and throw it away
      }
    }
    

    Yes. You can use your while loop instead of do-while. All that you need to add the if(!(cin>>answer)) { ... } in your code. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

bool is_something_ok(int param,SomeStruct* p) { bool is_ok = false; // check if is_ok if(is_ok)
bool fn() { if(something bad happen) return false; .. } void gn() { assert(something
bool isValid = false; string username = someadmin; If( !String.IsNullOrEmpty(username) && !( username.IndexOf(admin) !=
bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
-(BOOL) textFieldShouldReturn:(UITextField*) theTextField{ [theTextField resignFirstResponder]; return YES; } it is not working....
class Test { bool isVal() const { return isVal; } private: bool isVal; };
- (BOOL) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CocosNode* spHead = [self getChildByTag:tagHead]; CocosNode* spBody
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
Is accessing a bool field atomic in C#? In particular, do I need to
Out style: bool result; if(something.TryParse(val, out result)) { DoSomething(result); } Nullable style: bool? result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.