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(enteredDat a);
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(enteredDat a);
isGoodNumber = true;
}
catch(FormatException e)
{
Console.WriteLine("The number must be an integer");
}
}
}
}
}
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(enteredDat
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(enteredDat
isGoodNumber = true;
}
catch(FormatException e)
{
Console.WriteLine("The number must be an integer");
}
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
but it will be get out by the "break" statement in the else clause
ASKER
Oh I see, very good, thanks that could be a handy little tool.
ASKER
while(true)
the loops runs while 'what' is true?