?
Solved

error check in one loop

Posted on 2005-02-25
4
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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