pawelstack
pawelstack

Reputation: 1

Try/Catch and loop

1)After try and catch i want to loop it again to give new number("Console.ReadLine("Give correct number"))because user does not entered string convertable to double 2Second problem is, when user give wrong number i would like to loop again to enter new number. This version of program give message to small or to big number and exit

corrected

double number=10,11;
Console.WriteLine("Give a number");
string w=Console.ReadLine();
try
{
double d = Convert.ToDouble(w);
}
catch(FormatException)
{
Console.WriteLine("it is not a number");        
}
double dd=Convert.ToDouble(w);
if (dd == number)
{
Console.WriteLine("Yes, it is!");
}
else if (number<dd)
{
Console.WriteLine("to big number");
}
else if(number>dd)
{
Console.WriteLine("to small number");
}
Console.ReadLine();

Upvotes: 0

Views: 168

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 156978

First, use a do..while(condition) to keep asking until the user enters a valid number. Second, use TryParse to check if the value is valid. This is better than exception handling and converting it twice. Not sure why you are using doubles, but ints might be more appropriate.

bool valid = false;
do
{
    bool newValidState;
    Console.WriteLine("Give a number");

    string w = Console.ReadLine();

    double d;
    if (!double.TryParse(w, out d))
    {
        Console.WriteLine("it is not a number");
        newValidState = false;
    }
    else if (d == number)
    {
        Console.WriteLine("Yes, it is!");
        newValidState = true;
    }
    else if (wyliczona < wybor) // these conditions seem unrelated to `d`
                                // are they okay?
    {
        Console.WriteLine("to big number");
        newValidState = false;
    }
    else if(wyliczona > wybor)
    {
        Console.WriteLine("to small number");
        newValidState = false;
    }
    else
    {
        Console.WriteLine("unknown condition. needs work.");
        newValidState = false;
    }

    valid = newValidState;
}
while (!valid);

Note the use of newValidState, which will make sure you always assign a new value to valid. This helps to prevent endless loops due to never setting a value. The code will not compile unless every branch sets newValidState to a value.

Upvotes: 3

klashar
klashar

Reputation: 2563

Try this one:

var number =3;
do{
    Console.WriteLine("Give a number");
    string w=Console.ReadLine();
    try
    {
        double d = Convert.ToDouble(w);
    }
    catch(FormatException)
    {
        Console.WriteLine("it is not a number");
        continue; // not a number starting new iteration of the loop
    }
    double dd=Convert.ToDouble(w);
    if (dd == number)
    {
        Console.WriteLine("Yes, it is!");
        break; // The number guessed exiting loop
    }
    else if (dd>number)
    {
        Console.WriteLine("to big number");
    }
    else if(dd<number)
    {
        Console.WriteLine("to small number");
    }
}
while (true);
Console.ReadLine();

Upvotes: -1

Related Questions