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 7938543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:50:24+00:00 2026-06-03T22:50:24+00:00

I am having trouble validating user input, I have the following loop which definitely

  • 0

I am having trouble validating user input, I have the following loop which definitely catches it but when the loop starts again the user doesn’t have a chance to enter a different value so the value is the same and just creates an endless loop.

              private void guess_Click(object sender, EventArgs e)
    {


        int guessInt = 0;

        bool pass = false;
        int number;
        while (pass == false)
        {
            if (guessInput.Text != "")
            {
                pass = Int32.TryParse(guessInput.Text, out number);
                if (pass)
                {
                    guessInt = number;
                }
                else
                {
                    MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    guessInput.Text = "";
                }
            }
            else MessageBox.Show("You did not enter anything, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        guess.Enabled = false;
        next_Guess.Enabled = true;

        if (guessInt == randomArray[x])
        {
            result.Text = ("You Win! The correct number was " + randomArray[x]);
            right += 1;
            correctAnswers.Text = right.ToString();

        }
        else
        {
            result.Text = ("Sorry you lose, the number is " + randomArray[x]);
            wrong += 1;
            incorrectAnswers.Text = wrong.ToString();

        }

        hintLabel.Enabled = false;
        x++;
    }

So how can the user have a chance to reenter a value and the loop start again or should I be using a try/catch attempt here?

  • 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-06-03T22:50:25+00:00Added an answer on June 3, 2026 at 10:50 pm
    int number;
    if(string.IsNullOrEmpty(guessInput.Text))
    {
      MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
      return;
    }
    if(Int32.TryParse(guessInput.Text, out number))
    {
      guessInt = number; 
    }else
    {
      MessageBox.Show("You did not enter an integer, please enter a integer", "Invalid Values",      MessageBoxButtons.OK, MessageBoxIcon.Error);
       guessInput.Text = "";
       return;
    }
    
    
    // when come to here you have guessInt, process it 
    
       guess.Enabled = false;
        next_Guess.Enabled = true;
    
        if (guessInt == randomArray[x])
        {
            result.Text = ("You Win! The correct number was " + randomArray[x]);
            right += 1;
            correctAnswers.Text = right.ToString();
    
        }
        else
        {
            result.Text = ("Sorry you lose, the number is " + randomArray[x]);
            wrong += 1;
            incorrectAnswers.Text = wrong.ToString();
    
        }
    
        hintLabel.Enabled = false;
        x++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble validating my html, my menus are triggering the following error:
I am having trouble validating days with a reservation system I have to build
I've having trouble with parsing user input with Javascript, where I can't get the
I am having trouble validating my xml schema. I get these errors on the
I'm having trouble validating an HTML form with JavaScript. On their own they each
I'll admit, I'm an XML newbie. I'm having trouble validating some xml against a
I'm having trouble with some Excel data validation. On one sheet, I have a
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I am having trouble trying to pass an valuable after the user fail 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.