0

Can't figure out how to have the while loop running continuously for each input. Thanks for any help :)

Not sure if while (true) is needed or not, I'm still new to all of this :D

private static void AddToStock()
    {
        //Confirm item id is greater than 0
        int itemid = ReadInteger("\nItem ID:");
        string itemname = ReadString("Item Name:");
        int itemquantity = ReadInteger("Quantity:");
        double itemprice = ReadDouble("Price Paid:");
        DateTime itemdate = ReadDate("Date Added:");

        while (true)
            {
            if (itemid <= 0)
            {
                Console.WriteLine("Item ID Cannot Be Less Than 1, Please Try Again");
                itemid = ReadInteger("Item ID:");
                break;
            }

            //Confirm item quantity is greater than 0
            if (itemquantity <= 0)
            {
                Console.WriteLine("Quantity Cannot Be Less Than 1, Please Try Again");
                itemquantity = ReadInteger("Quantity:");
                break;
            }

            //Confirm item price is greater than 0.01
            if (itemprice < 0.01)
            {
                Console.WriteLine("Item Price Cannot Be Less Than 0, Please Try Again");
                itemprice = ReadDouble("Item Price:");
                break;
            }
        }

        //Add item to stock
        Employee_UI.AddToStock(itemid, itemname, itemprice, itemquantity, itemdate);
        Console.WriteLine("\nItem Added To Stock!");
    }
3
  • 1
    What do you mean by "a while loop for each user input"? Do you mean you want each one to separately loop until a valid value is entered? Sounds like that would be multiple loops, then. Commented Apr 12, 2020 at 20:35
  • @David I thought that would be the case, had it working like that before but wanted to try and find an alternative way Commented Apr 12, 2020 at 20:41
  • 1
    Have you asked a rubber duck? Perhaps you enter an invalid value for itemquantity. As you step through in the debugger, explaining to the rubber duck, you find that the code detects the problem and prompts the user to enter a correct value. Then it does an odd thing: it does a break instead of continue and doesn't validate the new input or subsequent parameters. You and the rubber duck look at each other and ... . Commented Apr 12, 2020 at 21:12

2 Answers 2

1

I would divide for each value you want to check individually:

            int itemid = ReadInteger("\nItem ID:");
            string itemname = ReadString("Item Name:");
            int itemquantity = ReadInteger("Quantity:");
            double itemprice = ReadDouble("Price Paid:");
            DateTime itemdate = ReadDate("Date Added:");

            while (itemid <= 0)
            {
                Console.WriteLine("Item ID Cannot Be Less Than 1, Please Try Again");
                itemid = ReadInteger("Item ID:");
            }
            //Confirm item quantity is greater than 0
            while (itemquantity <= 0)
            {
                Console.WriteLine("Quantity Cannot Be Less Than 1, Please Try Again");
                itemquantity = ReadInteger("Quantity:");
            }

            //Confirm item price is greater than 0.01
            while (itemprice < 0.01)
            {
                Console.WriteLine("Item Price Cannot Be Less Than 0, Please Try Again");
                itemprice = ReadDouble("Item Price:");
            }
            //Add item to stock
            Employee_UI.AddToStock(itemid, itemname, itemprice, itemquantity, itemdate);
            Console.WriteLine("\nItem Added To Stock!");

EDIT

Usually it is not recommended to have a while(true) loop, and a break inside to finish it, it is nicer to have a loop with a condition and inside the loop a way to accomplish this condition and end the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that in this case a do while may be more appropriate since the block is supposed to be executed at least once, but the answer is otherwise correct.
0

If you want to have each input to loop until a certain requirement is met then you will need more than one loop.

However there are clever ways to avoid multiple loops by writing your method as a recursive method.

If you are not familiar with recursively methods it is basically a method that calls itself inside the method. By using clever parameters you can use a recursively method as a loop. For example a simple recursive method

void loop(int index) {

   if(index == 10) { //recursive methods needs a terminating condition or else it would loop forever
       return;   //End loop
   } 
   System.out.println(index);
   loop(index++); //note that the method is called inside itself here
}

by calling loop(1) the numbers 1 to 9 will be printed.

Rewriting your method to a recursive method would be best to split in to two methods, one that is not recursive that intializes every parameter and one recursive method that does the actual looping.

Initalizing non recursive method

private static void AddToStock() 
{
   int itemid = ReadInteger("\nItem ID:");
   string itemname = ReadString("Item Name:");
   int itemquantity = ReadInteger("Quantity:");
   double itemprice = ReadDouble("Price Paid:");
   DateTime itemdate = ReadDate("Date Added:");

   AddToStockLoop(itemid, itemquantity, itemprice); //start the recursive loop with the initial values of the three parameters
}

and the looping recursive method

private static void AddToStockLoop(int itemid, int itemquantity, double itemprice) 
{

   if (itemid <= 0)
   {
      Console.WriteLine("Item ID Cannot Be Less Than 1, Please Try Again");
      itemid = ReadInteger("Item ID:");
      AddToStockLoop(itemid, itemquantity, itemprice); //Recursive call now with a new value of the paramter itemid
   }

   if (itemquantity <= 0)
   {
      Console.WriteLine("Quantity Cannot Be Less Than 1, Please Try Again");
      itemquantity = ReadInteger("Quantity:");
      AddToStockLoop(itemid, itemquantity, itemprice); //Recursive call now with a new value of the paramter itemquantity     
   }

   if (itemprice < 0.01)
   {
      Console.WriteLine("Item Price Cannot Be Less Than 0, Please Try Again");
      itemprice = ReadDouble("Item Price:");
      AddToStockLoop(itemid, itemquantity, itemprice); //Recursive call now with a new value of the paramter itemprice
   }
}

Writing recursive methods for small and simple programs are not necessarily beneficial but they can be very powerful for more complex programs where back-tracking is useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.