Link to home
Start Free TrialLog in
Avatar of jiggin23
jiggin23

asked on

error check in one loop

Below is the code I have created for a practice exercise.  My question is:  I was wondering if there was a way to do the error checking that I have done but do it all in one loop instead of two.  Thanks.

using System;

namespace ch8._2
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  string enteredData;
                  int number;
                  int quanity;
                  bool isGoodNumber = false;

                  while(!isGoodNumber)
                  {
                        try
                        {

                              Console.WriteLine("Please enter a stock number");
                              enteredData = Console.ReadLine();
                              number = Convert.ToInt32(enteredData);
                              isGoodNumber = true;
                        }
                        catch(FormatException e)
                        {
                              Console.WriteLine("The number must be an integer");
                        }

                  }

                  isGoodNumber = false;

                  while(!isGoodNumber)
                  {
                        try
                        {
                              Console.WriteLine("Please enter a quanity");
                              enteredData = Console.ReadLine();
                              quanity = Convert.ToInt32(enteredData);
                              isGoodNumber = true;
                        }
                        catch(FormatException e)
                        {
                              Console.WriteLine("The number must be an integer");
                        }

                  }
            }
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jiggin23
jiggin23

ASKER

sorry, dumb question, I am just learning this.  In the code above when you say:

while(true)

the loops runs while 'what' is true?
while "true" is true that means forever, at least that condition says that..

but it will be get out by the  "break" statement in the else clause
Oh I see, very good, thanks that could be a handy little tool.