5 Replies - 748 Views - Last Post: 22 January 2012 - 11:56 AM Rate Topic: -----

#1 rocker7859   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-January 12

Proper way to loop error condition

Posted 21 January 2012 - 04:17 PM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Proper way to loop error condition

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

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);

Was This Post Helpful? 0
  • +
  • -

#4 rocker7859   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-January 12

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.


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. :)
Was This Post Helpful? 0
  • +
  • -

#5 rocker7859   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-January 12

Re: Proper way to loop error condition

Posted 22 January 2012 - 10:50 AM

Hi,

I figured it out. Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer   User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1