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

  • SEARCH
  • Home
  • 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 7678811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:43:59+00:00 2026-05-31T17:43:59+00:00

I have such a program: #include <stdlib.h> #include <iostream> static int pswd=0; int main()

  • 0

I have such a program:

#include <stdlib.h>
#include <iostream>

static int pswd=0;

int main() {

    do {
        std::cout<<"I need your password:"<<std::endl;          
        std::cin>>pswd;
    } while (pswd!=3855);

    std::cout<<"Congratulations! Your password is correct! Your soul is free again!"<<std::endl;
}

And I have, may be, a stupid question.
When I enter invalid values (with non-numerical symbols or values greater then int) program goes in endless loop without reading of any information from console.

I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
        ...

Why does this program go in endless loop?

  • 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-31T17:44:00+00:00Added an answer on May 31, 2026 at 5:44 pm

    Because after invalid input, the stream is in failed state and all further input operations are no-op. You always have to check the result of the input operation.

    do {
        std::cout<<"I need your password:"<<std::endl;          
        if (!(std::cin >> pswd)) {
            // clear error flags
            std::cin.clear();
            // discard erroneous input (include <limits>)
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    } while (pswd!=3855);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this puny program. #include <iostream> #include <string> int main() { std::string st
On Visual Studio 2010 the following program #include <iostream> using std::cout; int main() {
I have a program that has this code : #include<stdio.h> main(){ int input; char
Suppose I have the following program: #include <stdio.h> int main() { printf(This is a
Please have a look on this code: #include <unistd.h> #include <stdlib.h> #include <stdio.h> int
So basically: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> int main(void){ //test strrev
I have a such syntax in program /* The Object1 is allowed to be
I have a requirement such that my program is suppose to run for a
In this program I am trying to make, I have an expression (such as
I tried implementing a sorting program in mapreduce such that I have just the

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.