Reputation: 1
im having issues with returning the desired value of x while its true to the if statement and otherwise would just start the do while cycle again, but my method is asking for a return value in catch parts of my code. Im trying to make a game where the player can input a value and the called method returns the value after its exceptions have been handled (in case the player inputs anything but an int, he gets an error message and is asked to try again).
Heres the code:
static int inputHumanCount()
{
Console.WriteLine("Input the number of people you want (current limit is 10000)");
bool key = true;
do
{
try
{
int x = Convert.ToInt32(Console.ReadLine());
if(x >= 1 && x <= 10000)
{
key = false;
return x;
}
else
{
Console.WriteLine("Wrong number");
}
}
catch (Exception)
{
Console.WriteLine("Wrong number");
}
} while (key == true);
Upvotes: 0
Views: 91
Reputation: 81593
Never ever use Convert
style methods or exceptions to validate user input.
Convert style methods throw and exception should not be use to control the flow of a process.
Instead user TryParse
style methods.
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
You could also do this succinctly in the while loop
Console.WriteLine("Input the number of people you want (current limit is 10000)");
string input = null;
int val = 0;
while (!int.TryParse(input = Console.ReadLine(),out val) || val < 1 || val > 10000)
Console.WriteLine($"Does this look like a number between 1 and 10000 to you? : {input}");
Console.WriteLine($"Game over you won : number = {val}");
Output
Input the number of people you want (current limit is 10000)
x
Does this look like a number between 1 and 10000 to you? : x
1231312
Does this look like a number between 1 and 10000 to you? : 1231312
10
Game over you won : number = 10
Note the point here isn't the code, it's to use TryParse
to validate input.
Upvotes: 1
Reputation: 385
You should return 0 in your code after do while loop, if you want to keep asking user to input some number and check if input was correct. Check this code:
static int inputHumanCount()
{
Console.WriteLine("Input the number of people you want (current limit is 10000)");
bool key = true;
do
{
try
{
int x = Convert.ToInt32(Console.ReadLine());
if (x >= 1 && x <= 10000)
{
key = false;
return x;
}
else
{
Console.WriteLine("Wrong number");
}
}
catch (Exception)
{
Console.WriteLine("Wrong number");
}
} while (key == true);
return 0;
}
Upvotes: 0
Reputation: 550
You could return the result after the do-while loop.
For example,
static int inputHumanCount()
{
Console.WriteLine("Input the number of people you want (current limit is 10000)");
int result = -1;
bool key = true;
do
{
try
{
result = Convert.ToInt32(Console.ReadLine());
if (result >= 1 && result <= 10000)
{
key = false;
break;
}
else
{
Console.WriteLine("Wrong number");
}
}
catch (Exception)
{
Console.WriteLine("Wrong number");
}
} while (key == true);
return result;
}
Upvotes: 1