noob Q
In c# console, I want to obtain 3 values (like a simple loan calculator) and verify that they're valid. If not require the user to re-enter. I don't want to init them prior to input so using something like while isn't a good option. If i do if-else, it seems like I'll have a messy nest not to mention a lot of redundancy. What technique do you suggest?
Proper way to loop error condition
Page 1 of 15 Replies - 748 Views - Last Post: 22 January 2012 - 11:56 AM
Replies To: Proper way to loop error condition
#2
Re: Proper way to loop error condition
Posted 21 January 2012 - 04:21 PM
Quote
I don't want to init them prior to input so using something like while isn't a good option.
Why?
Quote
What technique do you suggest?
I would suggest you declare your variables up front... and at the head of your while loop initialize them. The loop should have a boolean flag to determine if the input is valid.. if the input is not valid start the loop over.. if it is flip the flag and exit the while loop.
There is no need to make this overly complicated.
#3
Re: Proper way to loop error condition
Posted 21 January 2012 - 05:56 PM
since you always want to do the input at least one time, I'd use the do/while construct:
Boolean invalidInput = true;
do {
// get value
// check if value is good, if it is, set invalidInput to false
} while (invalidInput);
#4
Re: Proper way to loop error condition
Posted 22 January 2012 - 10:11 AM
Hi,
Thanks for the feedback. This works but I'd like to only require the user to re-enter the wrong value and not require all 3 like the code below. when they're all correct, it jumps to the calculation, which is perfect.
I'm probably over complicating it and missing something basic.
It's a simple program to help me get started and my end goal is allowing the user to enter values and qualifying each one before moving on. Thanks for the help. I prefer to learn the right way to code from the beginning so your input is appreciated.
double.TryParse(n, out numYears); - should be "years" and is correct in my code.
I do have "years" and not numYears in my code correctly. Above shows two different ones. Just wanted to clarify.
Thanks for the feedback. This works but I'd like to only require the user to re-enter the wrong value and not require all 3 like the code below. when they're all correct, it jumps to the calculation, which is perfect.
I'm probably over complicating it and missing something basic.
do
{
Console.Write("Enter the the princicpal for this loan: $ ");
p = Console.ReadLine();
double.TryParse(p, out loanAmount);
Console.Write("Enter the interest rate (in percent): ");
r = Console.ReadLine();
double.TryParse(r, out rate);
Console.Write("Enter the number of years for the proposed mortgage: ");
n = Console.ReadLine();
double.TryParse(n, out numYears);
if (locanAmount> 0 && rate > 0 && years> 0)
{
isNotValid = false;
}
else
{
isNotValid = true;
Console.WriteLine("You entered an invalid number. Plesae try again." + "\n");
}
}
while (isNotValid);
double.TryParse(n, out numYears); - should be "years" and is correct in my code.
I do have "years" and not numYears in my code correctly. Above shows two different ones. Just wanted to clarify.
#5
Re: Proper way to loop error condition
Posted 22 January 2012 - 10:50 AM
Hi,
I figured it out. Thanks.
I figured it out. Thanks.
#6
Re: Proper way to loop error condition
Posted 22 January 2012 - 11:56 AM
When posting code, post it in [ code ] tags. And also could you share your solution?
Page 1 of 1

New Topic/Question
Reply


MultiQuote






|