xslipstream
xslipstream

Reputation: 153

Breaking out of a loop when user inputs quit or exit c#

I'm having trouble with looping my program so that it breaks out of the loop if user enters "quit" or "exit".

When I type any string, my program crashes as it tries to parse the string input to an int. Any advice?

    namespace DiceRolling
    {
    /* We’re going to write a program that makes life easier for the player of a game like this. Start the
    program off by asking the player to type in a number of dice to roll.Create a new Random
    object and roll that number of dice.Add the total up and print the result to the user. (You should
    only need one Random object for this.) For bonus points, put the whole program in a loop and allow them to keep typing in numbers
    until they type “quit” or “exit”.  */

    class DiceRolling
    {
        static void RollDice(int numTries) 
        {
            Random random = new Random();

            //Console.Write("Please enter the number of dice you want to roll: ");
            //int numTries = Convert.ToInt32(Console.ReadLine());
            int sum = 0;
            int dieRoll;

            for (int i = 1; i <= numTries; i++)
            {
                dieRoll = random.Next(6) + 1;
                Console.WriteLine(dieRoll);
                sum += dieRoll;
            }
            Console.WriteLine("The sum of your rolls equals: " + sum);
        }

        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("Please enter the number of dice you want to roll: ");
                string input = Console.ReadLine();
                int numTries = Convert.ToInt32(input);
                //bool isValid = int.TryParse(input, out numTries);
                RollDice(numTries);
                Console.ReadKey();
                if (input == "quit" || input == "exit")
                    break;
            }
        } 
    }   
}

Upvotes: 0

Views: 7576

Answers (4)

Stack_Resp_seeker
Stack_Resp_seeker

Reputation: 31

Try int numTries =int.Parse(input != null ? input: "0");

Upvotes: 0

Eric Lizotte
Eric Lizotte

Reputation: 224

Tigrams is pretty close, this is slightly better

Console.Write("Please enter the number of dices you want to roll: ");
string input = Console.ReadLine();
while (input != "quit" && input != "exit")
{
  int numTries = 0;
  if (int.tryParse(input, out numTries)
  {
    RollDice(numTries);
  }
  Console.Write("Please enter the number of dices you want to roll: ");
  input = Console.ReadLine();
}

Upvotes: 3

Tigran
Tigran

Reputation: 62248

    while (true)
    {
        Console.Write("Please enter the number of dices you want to roll: ");
        string input = Console.ReadLine();
        if (input == "quit" || input == "exit") //check it immediately here 
            break;

        int numTries = 0; 
        if(!int.TryParse(input, out numTries)) //handle not valid number
        {
           Console.WriteLine("Not a valid number");
           continue;
        }


        RollDice(numTries);
        Console.ReadKey();

    }

Upvotes: 1

Innat3
Innat3

Reputation: 3576

tried to make it as similar as possible to what you have

    while (true)
    {
        Console.Write("Please enter the number of dices you want to roll: ");
        string input = Console.ReadLine();
        int numTries;
        if (int.TryParse(input, out numTries))
        {
            RollDice(numTries);
        }
        else if(input == "quit" || input == "exit")
        {
            break;
        }
    }

Upvotes: 1

Related Questions