[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

error check in one loop

Posted on 2005-02-25
4
Medium Priority
?
222 Views
Last Modified: 2010-04-16
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");
                        }

                  }
            }
      }
}
0
Comment
Question by:jiggin23
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Jesse Houwing earned 200 total points
ID: 13407620
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 hasNumber = false;

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

Author Comment

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

while(true)

the loops runs while 'what' is true?
0
 
LVL 2

Expert Comment

by:fromeroj
ID: 13407963
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
0
 

Author Comment

by:jiggin23
ID: 13407969
Oh I see, very good, thanks that could be a handy little tool.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question