Asking
Asking

Reputation: 265

Why does a foreach loop in infinite loop go on forever?

I'm trying to implement a recipe manager where the user can choose how they sort each recipe. Based on the user's input, I have a foreach loop going over each item in the Recipes list, which should iterate over each of them, and sorting them if necessary. Right now, I have an infinite loop which would let me keep letting the user enter an input if necessary, but when I attempt to view the list, it continues to spew out the results over and over again.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace recipeManager
{
    class Program
    {
        static List<recipes> Recipes = new List<recipes>()
        {
            new recipes() { recipeName = "Meatloaf", recipeType = "Dinner", listOfIngredients = new List<string> { "Ground beef", "Egg", "Onion", "Milk" } },
            new recipes() { recipeName = "PBnJ Sandwich", recipeType = "Lunch", listOfIngredients = new List<string> { "Peanut Butter", "Jelly", "Bread" } },
            new recipes() { recipeName = "Cake", recipeType = "Dessert", listOfIngredients = new List<string> { "Flour", "Sugar", "Baking Powder" } },
            new recipes() { recipeName = "Key lime pie", recipeType = "Dessert", listOfIngredients = new List<string> { "Key Lime Juice", "Egg Yolk", "Milk" } },
            new recipes() { recipeName = "Jambalaya", recipeType = "dinner", listOfIngredients = new List<string> { "Crawfish", "Egg Yolk", "Milk" } }
        };

        static void Main(string[] args)
        {

            Console.WriteLine("Hi there, how would you like to view our recipes? Enter View All for the");
            Console.WriteLine("entire recipe book, Sort Name to sort the recipes by name, Sort Type to sort");
            Console.WriteLine("the recipes by type, Sort Ingredients to sort by ingredients, break to break");
            Console.WriteLine("the loop");
            var input = Console.ReadLine();

            while (true)
            {
                switch (input)
                {
                    case "View All":
                        printAll();
                        break;
                    case "Sort Name":
                        sortName();
                        break;
                    case "Sort Ingredients":
                        sortIngredients();
                        break;
                    case "break":
                        return;
                    default:
                        Console.WriteLine("Not a valid command, please try again");
                        break;
                }
            }
        }

        //print entire list
        static void printAll()
        {
            foreach (recipes recipeProp in Recipes)
            {
                Console.WriteLine(recipeProp.recipeName + ", " + recipeProp.recipeType + ", " + "[" + String.Join(", ", recipeProp.listOfIngredients) + "]");
            }
        }

        //sort by name
        static void sortName()
        {
            var orderedNames = Recipes.OrderBy(l => l.recipeName);

            foreach (recipes recipeNames in orderedNames)
            {
                Console.WriteLine(recipeNames.recipeName + ", " + recipeNames.recipeType + ", " + "[" + String.Join(", ", recipeNames.listOfIngredients) + "]");
            }
        }

        //sort by type
        static void sortType()
        {
            var orderedType = Recipes.OrderBy(t => t.recipeType);

            foreach (recipes recipeTypes in orderedType)
            {
                Console.WriteLine(recipeTypes.recipeName + ", " + recipeTypes.recipeType + ", " + "[" + String.Join(", ", recipeTypes.listOfIngredients) + "]");
            }
        }

        static void sortIngredients()
        {

        }
    }
}

Removing the while worked, but I don't understand why it would loop forever, because foreach does not go on forever, and I even had a break statement that should have broken when foreach was finished.

Upvotes: 0

Views: 1187

Answers (2)

Jose Tepedino
Jose Tepedino

Reputation: 1584

@Asking,

You should put the readLine inside the loop, so there will be a stop to wait for user input.

   while (true)
   {
      Console.WriteLine(...);
      var input = Console.ReadLine();
      ...
   }

By doing this, the program will always present a message and ask for an input before printing anything else.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273284

//var input = Console.ReadLine();

while (true)
{
    var input = Console.ReadLine();  // put it here

    switch (input)
    {

Upvotes: 4

Related Questions