-3

I am on my final project in c# for beginners, but I can't seem to get my list to work properly. It also closes automatically after being 'filled' with orders.

class Burgare
{
    private string[] hamburger = new string[24];
    private int no_burger = 0;





    public void add_burger()//Ordering of burgers
    {
        Console.Clear(); //Clears console to make it look cleaner
        while (true)
        {
            int Burger_option = 0;
            do
            {
                Console.WriteLine("");// The options user can choose from
                Console.WriteLine("Please choose burgers from our menu:");
                Console.WriteLine("-------Burgers--------");
                Console.WriteLine("1 Original One     109");
                Console.WriteLine("2 Pig & Cow        109");
                Console.WriteLine("3 Spice & Nice     109");
                Console.WriteLine("4 Green One        109");
                Console.WriteLine("0 Go back to main menu");
                Console.WriteLine("----------------------");
                Console.WriteLine("");



                try //Making sure the user only picks what is on the menu
                {
                    Burger_option = int.Parse(Console.ReadLine());
                }

                catch
                {
                    Console.WriteLine("You can only choose what is on the menu!");


                }

                switch (Burger_option) //Depending on the number user presses on keyboard different burgers will be added to order
                {
                    case 1:
                        Console.WriteLine("You've added the Original One to your order");
                        Console.WriteLine("If you're done press 0 to go back to the main menu");
                        hamburger[no_burger] = "Original One";
                        break;

                    case 2:
                        Console.WriteLine("You've added the Pig & Cow to your order");
                        Console.WriteLine("If you're done press 0 to go back to the main menu");
                        hamburger[no_burger] = "Pig & Cow";
                        break;

                    case 3:
                        Console.WriteLine("You've added the Spice & Nice to your order");
                        Console.WriteLine("If you're done press 0 to go back to the main menu");
                        hamburger[no_burger] = "Spice & Nice";
                        break;

                    case 4:
                        Console.WriteLine("You've added the Green One to your order");
                        Console.WriteLine("If you're done press 0 to go back to the main menu");
                        hamburger[no_burger] = "Green One";
                        break;

                    case 0: //Sends user back to main menu
                        Run();
                        break;
                }
                no_burger++; //Adds burger

            } while (no_burger != 0);

            if (no_burger <= 25) //Maximum number of orders
            {
                Console.WriteLine("The restaurant can't take more than 24 orders of burgers at the time");
                Run(); //Sends user back to main menu when the order is over 24
            }
        }
    }



    public void print_order()
    {

        Console.WriteLine("-Your current order-");



    foreach (var burger in hamburger) //Showing a list of what is in the order
        {
         Console.WriteLine(hamburger);

         if (burger != null)

                Console.WriteLine(burger);            
         else
                Console.WriteLine("Empty space"); //Making empty space to show where you can add more burgers
        }

    }

After entering 24 orders I get the error "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" I've looked around a bit but still don't really understand how to fix my code. I'd like for it to return to the main menu after telling the user the order is full.

For my second problem the System.String [] problem, it appears when I enter the 'print_order'. It shows itself as System.String [] Empty space System.String [] Empty space And so on. If possible I'd like to either remove it completely or at least replace it with 1,2,3...etc.

  • Remove `Console.WriteLine(hamburger);` – Blorgbeard May 30 '19 at 16:18
  • 2
    For the first problem, it'll be better for you to debug and find out the problem yourself (add a breakpoint after adding 23 items). For the second problem, you might want to remove `Console.WriteLine(hamburger);` from the loop – Camilo Terevinto May 30 '19 at 16:18
  • Add in you main at end a Console.ReadLine() so program doesn't terminate. – jdweng May 30 '19 at 16:21

2 Answers2

5

First off, please only post one question per question; you've described many problems here, but only actually asked one question. If you have multiple questions, post multiple questions.

Why do I get System.String[] in my code while debugging?

C# by default gives the name of the type when you print it out, and you're printing an array of strings. To turn an array of strings into a string, use the Join method of the string type. Be careful to not confuse it with the Join extension method on sequences.

It also closes automatically after being 'filled' with orders.

When a console program's control reaches the end of Main, it terminates the program. If you want something else to happen, write code that indicates what you'd like to happen.

After entering 24 orders I get the error "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" I've looked around a bit but still don't really understand how to fix my code. I'd like for it to return to the main menu after telling the user the order is full.

You've reserved space for 24 things, and you've tried to access the 25th thing. That's a fatal error. Don't do that. You are required to detect that situation and prevent it.

If you want it to return to the main menu after telling the user the order is full then write code to do that. How? Since apparently the main menu is a thing that can happen more than once, you'll want to put it in a loop. You've already written two loops; put more stuff in the loops!

While we're looking at your code, some more good advice:

  • Start using the conventions of the C# language now. You wrote Console.WriteLine and then made a method print_order. Follow the pattern set by the designers of the framework, not something you've just made up on your own. Types and methods should be CasedLikeThis.
  • Never put an int.Parse in a try-catch, ever. That's what int.TryParse is for!
  • Your program has a bug; what happens if someone types in, say, 7? Handle that case!
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • The OP put the logic to check to see if they've reached the maximum number of items outside of the `do...while` loop, so it doesn't stop the user from continuing to try to add stuff even after they've reached the maximum number (unless the user happens to have entered '0' at exactly the right time). – EJoshuaS - Stand with Ukraine May 30 '19 at 18:54
  • @EJoshuaS: That's what I was hinting at when I said "put more stuff in the loop" and "you are required to detect that situation". – Eric Lippert May 30 '19 at 19:36
  • I figured that's probably what you were referring to... just being more explicit about what the problem is since the OP's new to programming. – EJoshuaS - Stand with Ukraine May 30 '19 at 19:37
0

You never actually check to see how many items you've added inside your do...while loop - you only check this after the loop is complete. That's why it crashes when you try to add the 25th item.

Move the logic to handle this inside your do...while loop and you won't get the exception anymore.

Also, your array only has room for 24 items, but I assume that you meant for it to be 25.

Also, since you're new to programming, you may want to look at @Eric Lppert's helpful article on debugging techniques - it'll save you a lot of time. You should also read about how to use a step debugger to diagnose problems.