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,286
  • 1
  • 11
  • 24
NiallUK
  • 87
  • 10
  • 8
    Search how to create a `while` loop. – Johnny Mopp Apr 09 '20 at 14:14
  • 2
    and how to check for a "between"-condition – HimBromBeere Apr 09 '20 at 14:16
  • 1
    You need a loop, do not throw an exception, do not use Convert, use TryParse, use an integer var and use <= and >= operators. – Fildor Apr 09 '20 at 14:16
  • 4
    [Don't use exception for a flow control](https://stackoverflow.com/q/729379/1997232). `ArgumentException` is when method receive a wrong argument. If you would call a method (inside `try/catch`) that would be fine, but to organize a loop - don't. – Sinatr Apr 09 '20 at 14:17
  • Thanks for your feedback all. I will do some more research! – NiallUK Apr 09 '20 at 14:19

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
  • 55,981
  • 11
  • 86
  • 129
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
  • 836
  • 7
  • 25
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
  • 11,419
  • 4
  • 29
  • 57
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
  • 728
  • 8
  • 21