Home > Enterprise >  C# looping user input and adding the total from each selection they make
C# looping user input and adding the total from each selection they make

Time:09-23

I am attempting to write a program that asks the user for an item then displays the price using a dictionary. I need to loop the program so it repeats itself unless the user types "End" with any variation and it also needs to add the prices together for each item the user enters.

I have been stuck on this for several days any help would be awesome.

//dictionary using food and prices 
Dictionary<string, double> FoodMenu = new Dictionary<string, double>();
//add food and prices
FoodMenu.Add("Baja Taco", 4.00);
FoodMenu.Add("Burrito", 7.50);
FoodMenu.Add("Bowl", 8.50);
FoodMenu.Add("Nachos", 11.00);
FoodMenu.Add("Quesadilla", 8.50);
FoodMenu.Add("Super Burrito", 8.50);
FoodMenu.Add("Super Quesadilla", 9.50);
FoodMenu.Add("Taco", 3.00);
FoodMenu.Add("Tortilla Salad", 8.00);

//get order

//food input string

string foodItem = getfooditem();

//use string
static string getfooditem()
{
    Console.WriteLine("Item:");
    string foodItem = Console.ReadLine();
    return foodItem;
}

//check if key exists in food menu
string searchMenu = foodItem;

if (FoodMenu.ContainsKey(searchMenu))
{
    Console.WriteLine($"Total: ${FoodMenu[searchMenu]}");
}
else
{
    Console.WriteLine($"{searchMenu} does not exist in the menu.");
}

CodePudding user response:

If you want a program to repeat itself, you need a loop structure, in this case, a while loop since the number of iterations is not fixed or known. Something like this should do the trick

string userInput = GetFoodItem();
while (userInput != "End")
{
    if (foodMenu.ContainsKey(userInput))
    {
        Console.WriteLine($"Total: ${FoodMenu[userInput]}");
    }
    else
    {
        Console.WriteLine($"{userInput} does not exist in the menu");
    }
    userInput = GetFoodItem();
}

At the end of the loop, we're asking the user for another food item and then checking it immediately after. If it so happens to be 'End' then the while loop will not execute, and will continue to the code succeeding it

CodePudding user response:

Because we see these kinds of questions a lot I thought I'd give an alternative approach:

foreach (var response in
    Enumerable
        .Repeat(0, int.MaxValue)
        .Select(x => Console.ReadLine())
        .TakeWhile(x => x != "End")
        .Select(x =>
            FoodMenu.ContainsKey(x)
            ? $"Total: ${FoodMenu[x]}" 
            : $"{x} does not exist in the menu."))
    Console.WriteLine(response);

CodePudding user response:

try this one out :-)

pretty similar with the first answer by @MoonMist however using DoWhile, and with OrdinalIgnoreCase to compare the end input to end the program.

 static void Main(string[] args)
    {
        Dictionary<string, double> FoodMenu = new Dictionary<string, double>();
        FoodMenu.Add("Baja Taco", 4.00);
        FoodMenu.Add("Burrito", 7.50);
        FoodMenu.Add("Bowl", 8.50);
        FoodMenu.Add("Nachos", 11.00);
        FoodMenu.Add("Quesadilla", 8.50);
        FoodMenu.Add("Super Burrito", 8.50);
        FoodMenu.Add("Super Quesadilla", 9.50);
        FoodMenu.Add("Taco", 3.00);
        FoodMenu.Add("Tortilla Salad", 8.00);

        string searchKey = string.Empty;

        do
        {
            if (!string.IsNullOrWhiteSpace(searchKey))
            {
                Console.WriteLine(FoodMenu.ContainsKey(searchKey)
                    ? $"Total: ${FoodMenu[searchKey]}"
                    : $"{searchKey} does not exist in the menu.");
            }

            searchKey = GetFoodItem();

        } while (!searchKey.Equals("end", StringComparison.OrdinalIgnoreCase));

        Console.WriteLine("----End of Program---");
    }


    //use string
    static string GetFoodItem()
    {
        Console.WriteLine("Item:");
        string foodItem = Console.ReadLine();
        return foodItem;
    }

hope it helps! Thank you..

  • Related