Search code examples
c#console-application

How to get user input in a C# console application and process it as items of an order?


I searched a lot in the internet but didn't find anything helpful I'm trying to get a number from the user and then use it to:

  1. Write the name of that product.
  2. At the end to show everything they want to order
  3. Calculate the price of the order.

So if they say 1, 2, 5 it prints "CPU, GPU, 100000". I just want to know how to add the second part to get input from user and then list all of them at the end.

string[] computer = { "Cpu", "Gpu", "Ram", "Hard", "Motherboard" };
int[] price = { 30000, 70000, 20000, 25000, 40000 };
Console.WriteLine("0 = Cpu , 1=Gpu , 2=Ram 3,=Hard 4=Motherboard", "each Time Put Number For Your Order , Put 5 To Calculate");

Start:

Console.Write("Enter What You Want To Order : ");
int num = Convert.ToInt32(Console.ReadLine());
List<string> list = new List<string>();
**list.Add(computer[num]);** (This is the part that shows the error
if (num < 5)
{
    Console.WriteLine(computer[num]);
    goto Start;
}
else if (num == 5)
{
    Console.WriteLine("Finished Proccess");
    goto Next;
}
else
{
    Console.WriteLine("Not Valid");
}

Next:

foreach (var lol in list)
{
    Console.WriteLine(lol);
}

I tried using list.add but it didn't work - list.Add(computer[num]);


Solution

  • I made a few changes to your code to make it work as per your requirements.

    First, as pointed out by @jdweng in a comment, you need to move the assignation of your List before Start:, because using Goto: you are coming back to that point and resetting the list to new() each iteration. Also note that you only need to store integers in the list to keep track of all the items you buy, so you can use a List<int> instead of a List<string>. I've called it order:

    List<int> order = new List<int>();
    
    Start:
    

    For the options if (num == 5) and else you are not buying anything so you need to .Add a new item to the orders only inside the if (num < 5) block, which is the case when you buy an item:

    if (num < 5)
    {
        Console.WriteLine(computer[num]);
        order.Add(num);
    
        goto Start;
    }
    

    Finally, in order to show the list of all your items in the order, since the two arrays computer and price loop through the list orders and for each order, print both the item name and the price. In each iteration, you also add the price to the variable totalPrice to keep track of the total cost of your order and print it in the end:

    Next:
    
    Console.WriteLine("You ordered:");
    
    int totalPrice = 0;
    
    foreach(var item in order)
    {
        Console.WriteLine($"\t{computer[item]} {price[item]}");
    
        totalPrice += price[item];
    }
    
    Console.WriteLine($"Total: {totalPrice}");
    

    All in all, the complete program looks like this:

    string[] computer = { "Cpu", "Gpu", "Ram", "Hard", "Motherboard" };
    int[] price = { 30000, 70000, 20000, 25000, 40000 };
    Console.WriteLine("0 = Cpu , 1=Gpu , 2=Ram 3,=Hard 4=Motherboard", "each Time Put Number For Your Order , Put 5 To Calculate");
    
    List<int> order = new List<int>();
    
    Start:
    
    Console.Write("Enter What You Want To Order : ");
    int num = Convert.ToInt32(Console.ReadLine());
    
    if (num < 5)
    {
        Console.WriteLine(computer[num]);
        order.Add(num);
    
        goto Start;
    }
    else if (num == 5)
    {
        Console.WriteLine("Finished Proccess");
        goto Next;
    }
    else
    {
        Console.WriteLine("Not Valid");
    }
    
    Next:
    
    Console.WriteLine("You ordered:");
    
    int totalPrice = 0;
    
    foreach(var item in order)
    {
        Console.WriteLine($"\t{computer[item]} {price[item]}");
    
        totalPrice += price[item];
    }
    
    Console.WriteLine($"Total: {totalPrice}");
    Console.ReadLine();
    

    I agree with @Narish that you should probably refrain from using Goto:, Start: and Next:, as it makes the code harder to read, and it makes your code brittle, unnecessarily convoluted and prone to bugs and brittle (see what is wrong with using goto?). Instead, you can control the execution flow of your program with while, for or foreach loops.

    Also, take into account that the index in C# arrays and collections are 0-based, which means that the first position is 0, the second position is 1, and so on. Therefore, in your example:

    So if they say 1, 2, 5 it prints "CPU, GPU

    1 and 2 correspond to Gpu and Ram, not to Cpu and Gpu.