0

Please see my code below.

Console.WriteLine("Enter a number between 1 and 5");
string _numberInt = Console.ReadLine();
Convert.ToInt32(_numberInt);

if (_numberInt != "1, 5")
{
    throw new System.ArgumentException("Please use a number between 1-5.");
}

I am trying to make the code so that if the user doesn't input a number between 1 and 5, it will throw an exception. I then want it to go back and ask the question again until it is correct.

At this current instance, it will simply skip past this and the console will close, while also ignoring the rest of my code. Please could someone help me understand what i need to do here?

Before anyone suggests anything, I've not been coding long, and i am trying to learn as much as possible. Please leave constructive advice.

Oguz Ozgul
  • 6,521
  • 1
  • 13
  • 25
NiallUK
  • 87
  • 11

4 Answers4

1

You need to use the >= and <= operators to check the input after you've tried to convert it from a string to a number:

while(true)
{
    Console.WriteLine("Enter a number between 1 and 5");
    var input = Console.ReadLine();

    if(int.TryParse(input, out var value) && value >= 1 && value <= 5)
    {
        Console.WriteLine("Thanks");
        break;
    }
    else
    {
        Console.WriteLine("invalid input")
    }
}

Note that you should never use exceptions as some sort of cheap flow control. They're for reporting exceptional situations which you cannot typically recover from. In your program an invalid value is not the exception, it is the norm and you should handle it appropriately.

Sean
  • 58,802
  • 11
  • 90
  • 132
0

You need a while loop to repeatedly check and make the user input a number until the condition is matched. This is what you want:

while (true) {
    Console.WriteLine("Enter a number between 1 and 5 > ");
    int _numberInt = Convert.ToInt32(Console.ReadLine());

    if (_numberInt <= 5 && _numberInt >= 1) { // Check if number is between 1 and 5
        break; // Number is between 1 and 5. Break.
    }
    else { // Number is not between 1 and 5
        throw new System.ArgumentException("Please use a number between 1-5.");
    }
}

Also note that throwing an uncaught exception will end the program, so you may just want to tell the user with Console.WriteLine() that they haven't entered a valid number.

CyanCoding
  • 1,000
  • 1
  • 13
  • 32
0
int uservalue;
for(;;) // what kind of loop you use doesn't really matter
{
    Console.WriteLine("Enter a number between 1 and 5");
    // use TryParse to validate and convert user input.
    // "continue" immediatly goes back to start of loop (= next iteration)
    if( !int.TryParse(Console.ReadLine(), out uservalue)) continue;
    // check for correct value range
    if(5 >= uservalue && uservalue >= 1) break;
}
Console.WriteLine("You entered {0}", uservalue);
Fildor
  • 12,873
  • 4
  • 34
  • 64
0

I think this should do what you need. It doesn't throw an exception though. It doesn't need to, does it?

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Ask();
            while (!(int.TryParse(input, out int result) && IsBetween1And5(result)))
            {
                input = Ask();
            }
            Console.WriteLine($"Thanks for entering {input}!");
        }

        private static bool IsBetween1And5(int value)
        {
            return value <= 5 && value >= 1;
        }

        private static string Ask()
        {
            Console.WriteLine("Enter a number between 1 and 5");
            return Console.ReadLine();
        }
    }
}
benjamin
  • 1,078
  • 11
  • 23