Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Hey everyone, newbie looking for some assistance with this code.

I have a program that works as intended with the but it closes out if there is a "invalid number " inputted. The program should allow the user to input a number between 1 - 50 and produce a message if the number inputted is higher than 50. The program will display the message but then closes out right after. Can anyone take a look and see what I am doing wrong. Using Visual Studio Community edition / Console.App (.NET Freamework.

class Program

  {

    static void Main(string[] args)

    {

      int count;

      // loop that continues until the user enters a number < 1 or > 50

      do

      {

        // input a whole number

        Console.Write("Enter a whole number between 1 and 50: ");

        count = Convert.ToInt32(Console.ReadLine());


        // check if user wants to exit

        if (count >= 1 && count <= 50)

        {

          // loop to display count number of asterisks

          for (int i = 0; i < count; i++)

            Console.Write("*");

          Console.WriteLine(); // display a new line at the end 

        }

        else if (count > 50)

        {

          Console.WriteLine("Invalid number, please enter a number between 1-50 only");

        }


      }

      while (count > 0 && count <= 50);

      Console.ReadKey();

Answers

  • You are checking line in the while is count being in between 0 to 50 so if the player inputs greater than 50 that means your condition is false it will take you out of the loop


    So you have to check for count being less then 0 or greater than 50 to continue the loop

Sign In or Register to comment.