Another quick question would it be best to use a If statement or a while loop?
26 Replies - 1410 Views - Last Post: 19 November 2015 - 01:06 PM
#17
Re: Need help with int.TryParse()
Posted 19 November 2015 - 09:48 AM
They are not comparable. If is a single conditional statement, while is a loop structure.
#18
Re: Need help with int.TryParse()
Posted 19 November 2015 - 09:56 AM
I know it is not the cleanest but this is what i got so far. But, it only puts the invalid data once then it goes through upon next line
private static int Month()
{
int month = 0;
string input = Console.ReadLine();
bool isSuccess = int.TryParse(input, out month);
if(isSuccess == false)
{
Console.Write("Invalid value please try again >> ");
input = Console.ReadLine();
}
else
{
while (month < MIN_MONTH || month > MAX_MONTH)
{
Console.Write("Invalid value. Please enter month again >> ");
input = Console.ReadLine();
}
}
#19
Re: Need help with int.TryParse()
Posted 19 November 2015 - 10:13 AM
I suggest testing your code instead of having us debug by proxy. Just eyeballing that you've got issues with an infinite loop in one case, or unexpected results in another.
#20
Re: Need help with int.TryParse()
Posted 19 November 2015 - 10:27 AM
I tried it usually skips over the while loop for the MIN_MONTH and MAX_MONTH
#21
Re: Need help with int.TryParse()
Posted 19 November 2015 - 10:30 AM
Try entering 26.
#22
Re: Need help with int.TryParse()
Posted 19 November 2015 - 10:34 AM
it catches it and gives me the invalid data message but when i hit a it stuck in a infinite loop lol.
#23
Re: Need help with int.TryParse()
Posted 19 November 2015 - 10:48 AM
Thanks everyone it works now everything i mean.
#24
Re: Need help with int.TryParse()
Posted 19 November 2015 - 11:00 AM
Good. You could post your final version for anyone interested.
#25
Re: Need help with int.TryParse()
Posted 19 November 2015 - 11:12 AM
private static int Month()
{
int month = 0;
string input = Console.ReadLine();
// If value is not numaric then it throws this error
while(!int.TryParse(input, out month))
{
Console.Write("Value is not numaric. Please enter month again >> ");
input = Console.ReadLine();
}
// If value is lower than 1 or higher than 12 it throws this error
while (month < MIN_MONTH || month > MAX_MONTH)
{
Console.Write("Invalid value. Please enter month again >> ");
month = Convert.ToInt32(Console.ReadLine());
}
return month;
}
This does not work if you happen to type a number to high for instance '26' in first then a letter.
This post has been edited by Hydroify: 19 November 2015 - 01:07 PM
#26
Re: Need help with int.TryParse()
Posted 19 November 2015 - 12:38 PM
LOL! So what happens if the user first enters "26" and then later enters "I Love Hydroify" ?
#27
Re: Need help with int.TryParse()
Posted 19 November 2015 - 01:06 PM
updated code works now
And Skydiver if '26' is entered it says Invalid value. Please enter month again. and if 'I Love Hydroify' is entered it says Value is not numeric. Please enter month again.
private static int Month()
{
int month = 0;
string userInput = Console.ReadLine();
bool isSuccess = false;
while (!isSuccess)
{
if (int.TryParse(userInput, out month))
// If value is lower than 1 or higher than 12 it throws this error
if(month < MIN_MONTH || month > MAX_MONTH)
{
Console.Write("Invalid value. Please enter month again >> ");
userInput = Console.ReadLine();
}
else
isSuccess = true;
else
{
// If value is not numaric then it throws this error
Console.Write("Value is not numaric. Please enter month again >> ");
userInput = Console.ReadLine();
}
}
return month;
}
And Skydiver if '26' is entered it says Invalid value. Please enter month again. and if 'I Love Hydroify' is entered it says Value is not numeric. Please enter month again.

New Topic/Question
Reply



MultiQuote

|