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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:54:34+00:00 2026-06-01T11:54:34+00:00

I am trying to develop a method which will check a users input, and

  • 0

I am trying to develop a method which will check a users input, and only return the input, if it passes validation.

this is what I want to do:

  1. User enters input
  2. Check value of input
  3. If input satisfies logic, then return that value, else call the function again.

this is really what I want, but the complier states that not all code paths return a value:

   public static int UserInput(){
   int input =  int.Parse(Console.ReadLine());
   if (input < 1 || input > 4){
       Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");
       if (input < 1 || input > 4)  UserInput();

   } else{
       return input; 
   }
}

However, this is the following code that satisfies the complier.

    public static int UserInput()
    {
       int input =  int.Parse(Console.ReadLine());
       if (input < 1 || input > 4)
       {
           Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");

           if (input < 1 || input > 4)
           {
               UserInput();
               return -1; // Never reached, but must be put in to satisfy syntax of C#
           }
           return input; // Never reached, but must be put in to satisfy syntax of C#
       }
       else
       {
           return input;

       }
    }

This kind of works, but I get strange results. If a user was to enter in an input that is either 1,2,3 or 4 in the first go (i.e if statement evaluates to false), then the returned input is whatever the user entered. However, if the user was to enter a value that was not 1,2,3 or 4 then enter a valid number, then program would do the following:

  1. return input;
  2. jump into the child if statement and run UserInput();
  3. then return -1.
  • 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-01T11:54:35+00:00Added an answer on June 1, 2026 at 11:54 am

    You need to return UserInput(); by the looks of it. It simply looks like a recursive function that will drill down and return at the bottom by continually calling itself until a satisfactory constaint is met.

    What you are doing is drilling down, letting it return a value, then returning a -1 on top of that.

    You are also duplicating yourself by checking input again. It looks like this could be boiled down to the following:

    public static int UserInput()
    {
       int input =  int.Parse(Console.ReadLine());
       if (input < 1 || input > 4)
       {
           Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");
           return UserInput();
       }
       else
           return input;
    }
    

    So, what will happen is that if the user enters an invalid number, it will call itself again. If they then enter a valid number. The method will return to the first call, which will take that value and return it back up to the original call.

    Here is how a recursive call using this would look like:

    CallingMethod calls UserInput(0)
    -UserInput(0)
    --UserInput(5)
    ---UserInput(2) return 2
    --UserInput(5) return 2
    -UserInput(0) return 2
    CallingMethod receives and uses 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a class that supports an asynchronus method invocation. This is
I am trying to develop an XSLT stylesheet which will transform an xml into
I am trying to develop a small app BusTracker which will list down a
I am trying to develop a simple map application, which will display a map
Using java I am trying to develop a method using recursion to analyze a
I am trying develop a basic referrer system to my Django website, system will
I am trying to develop a PHP class which would enable me to get
I'm trying to develop an application where in the people will get notified if
I am trying to develop a plugin architecture in .Net. The application will be
I'm trying to develop some web page in Asp.Net and C# which almost look

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.