Question Simple console; save, show & delete items and exit (Backpack)

NisseMax

New member
Joined
May 27, 2020
Messages
4
Programming Experience
Beginner
Hi everybody! I'm new when it comes to programming so my coding skills are not the best. I would appreciate if you who comments can take this in consideration.

The problem that I have is that the string called "item" isn't getting recognized when I want to use it when the user wants to save an item. I've tried placing the string "item" in different codeblocks to see if that changes it but I get no success. What is the problem here? By the way the string is at the current moment in line 33 and the option to save an item is in line 89 with the item = Console.ReadLine( ); in line 90. The option to show item is in line 97 to 98.

The code is divided in several methods for structure purposes.

FYI the program is not fully done yet!


C#:
using System;

namespace BackPack
{
    class Program
    {
        static void Main(string[] args)
        {
            //Intro to the console
            Console.WriteLine("\n\tHi and welcome to Backpack!");
            Console.WriteLine("\t----------------------------------");
            Console.Write("Please hit enter to continue to the menu. . .");
            Console.ReadLine();

            //Tellin c# to head over the following method named "WhileSwitch()"
            WhileSwitch();
        } //End Main

        static void WhileSwitch()
        {
            Console.Clear();

            //main; loops through menu and calls method based on choice
            //Declaring a stringvariable named optionChoice for the while loop condition.
            string optionChoice;

            //this is called the priming read
            //initializes the loop control variable (choice)
            //before loop condition

            //Tellin c# to head over the following method named "DisplayMenu()"
            DisplayMenu();
            string item = "";
            optionChoice = Console.ReadLine();

            //While loop condition.
            while (optionChoice != "5")
            {
                switch (optionChoice)
                {
                    case "1":
                        OptionSaveItems();
                        break;

                    case "2":
                        OptionShowItems();
                        break;

                    case "3":
                        OptionBackpackCleanUp();
                        break;

                    case "4":
                        OptionExitBackpack();
                        break;

                    default:
                        {
                            Console.WriteLine("Please choose one of the for options presented in the menu!");
                            Console.Clear();
                            DisplayMenu();
                            break;
                        }//End default.

                }//End switch.
                //Out of the swith here, but still in the loop.
                //Re-display the menu because the user has done one of the items.

                Console.Clear();
                DisplayMenu();
                optionChoice = Console.ReadLine();

            }//End while.
        }//End Statements

        static void DisplayMenu()
        {
            Console.WriteLine("Down below you have four options to choose from.");
            Console.WriteLine("\n\t [1] - Save items in the backpack.");
            Console.WriteLine("\n\t [2] - Show saved items in the backpack.");
            Console.WriteLine("\n\t [3] - Clean up backpack.");
            Console.WriteLine("\n\t [4] - Quit backpack.");

        }//End DisplayMenu

        static void OptionSaveItems()
        {
            //Giving the user the ability to save items.
            Console.Write("You selected option 1.\n\nType in the desired item you wish to save: ");
            //Reading the users input and assigning that value to the variale "item".
            item = Console.ReadLine();

        } //End OptionSaveItems

        static void OptionShowItems()
        {
            Console.Write("You selected option 2.\n\nYou have saved following item/items: ");
            Console.WriteLine(item);

        } //End OptionShowItems

        static void OptionBackpackCleanUp()
        {
            Console.Write("You selected option 3.\nThe backpack is getting cleaned of items. . .");
            Console.WriteLine();
            Console.Write("\tEmpty!\n\nHit any key to return to the menu again. . .");
            Console.ReadKey();

        } //End OptionBackpackCleanUp

        static void OptionExitBackpack()
        {
            Console.Write("The program is ending. . .");

        } //End OptionExitBackpack
    }
}
 
That item variable is declared in the WhileSwitch method so it only exists in that method. As far as code elsewhere is concerned, there is no such variable. If you want to be able to refer to a variable in multiple methods then it must be declared outside all of them. All your methods are static, which means that they can only refer to other static members, so you have to declare that variable static in order for those methods to be able to use it.
 
Hi jmcilhinney! Thanks for replying and giving some advice.

Yes that is correct. I thought that the item variable should be declared before the while loop so that's why I declared it in the codeblock for the WhileSwitch method. Okay so if I understand correctly. When you say outside of the methods wouldn't that be in the codeblock right after "class Program"? Sorry but I'm not sure that I really understand your explanation. Im a total noob when it comes to programming.
 
I'm not quite sure what's confusing about the word "outside". If you know what it means for a variable declaration to be inside a method then you must know what it means for it to be outside.
 
Hmm you may be right. It wouldn't be hard to understand the difference between inside and outside but I sometimes have difficulties understanding what's inside and outside when there several codeblocks. It's just me that don't really have a long experience of coding and therefore not the best confidence. I just wanted to be sure on what you meant.

But thanks anyway. I will try to work it out.
 
Just make sure that your code is properly indented, which it appears to be, and it should be quite simple to determine what is in which code block. You can always collapse your method bodies too, so you know that anything you can still see is outside them all.
 
I got most of the things to work now but now I have one small problem and that is that the console seems not recognize the option 4 in the OptionExitBackpack method which is for quitting the app. Any suggestions on that?

C#:
using System;

namespace BackPack
{
    class Program
    {
        static string item;
        
        static void Main(string[] args)
        {
            //Intro to the console
            Console.WriteLine("\n\tHi and welcome to Backpack!");
            Console.WriteLine("\t----------------------------------");
            Console.Write("Please hit enter to continue to the menu. . .");
            Console.ReadLine();

            //Tellin c# to head over the following method named "WhileSwitch()"
            WhileSwitch();
        } //End Main

        static void WhileSwitch()
        {
            Console.Clear();

            //main; loops through menu and calls method based on choice
            //Declaring a stringvariable named optionChoice for the while loop condition.
            string optionChoice;

            //this is called the priming read
            //initializes the loop control variable (choice)
            //before loop condition

            //Tellin c# to head over the following method named "DisplayMenu()"
            DisplayMenu();
            string item = "";
            optionChoice = Console.ReadLine();

            //While loop condition.
            while (optionChoice != "5")
            {
                switch (optionChoice)
                {
                    case "1":
                        OptionSaveItems();
                        break;

                    case "2":
                        OptionShowItems();
                        break;

                    case "3":
                        OptionBackpackCleanUp();
                        break;

                    case "4":
                        OptionExitBackpack();
                        break;

                    default:
                        {
                            Console.WriteLine("Please choose one of the for options presented in the menu!");
                            Console.Clear();
                            DisplayMenu();
                            break;
                        }//End default.
                }//End switch.
                
                //Out of the swith here, but still in the loop.
                //Re-display the menu because the user has done one of the items.

                Console.Clear();
                DisplayMenu();
                optionChoice = Console.ReadLine();
            }//End while.
            
        }//End Statements

        static void DisplayMenu()
        {
            Console.WriteLine("Down below you have four options to choose from.");
            Console.WriteLine("\n\t [1] - Save items in the backpack.");
            Console.WriteLine("\n\t [2] - Show saved items in the backpack.");
            Console.WriteLine("\n\t [3] - Clean up backpack.");
            Console.WriteLine("\n\t [4] - Quit backpack.");
        }//End DisplayMenu

        static void OptionSaveItems()
        {
            //Giving the user the ability to save items.
            Console.WriteLine("You selected option 1.")
            Console.WriteLine("\nType in the desired item you wish to save: ");
            //Reading the users input and assigning that value to the variale "item".
            item = Console.ReadLine();
        } //End OptionSaveItems

        static void OptionShowItems()
        {
            Console.Clear();
            Console.Write("You selected option 2.")
            Console.WriteLine("\nYou have saved following item/items: ");
            Console.WriteLine(item);
            
            //If user don't have any saved items then this text will occur.
            if (item == " ")
            {
                Console.Clear();
                Console.WriteLine("You don't have any saved items at the moment!");
            }
        } //End OptionShowItems

        static void OptionBackpackCleanUp()
        {
            Console.Clear();
            item = " ";
            Console.Write("You selected option 3.\nThe backpack is getting cleaned of items. . .");
            Console.WriteLine();
            Console.Write("\tEmpty!\n\nHit any key to return to the menu again. . .");
            Console.ReadKey();
        } //End OptionBackpackCleanUp
        
        //THIS IS WHERE THE PROBLEM IS!
        static void OptionExitBackpack()
        {
            Console.Write("The program is ending. . .");

        } //End OptionExitBackpack
    }
}
 
What is the problem? Are you getting an error? If you aren't getting an error, what behaviour are you seeing? What behaviour were you expecting to see?
 
Have you actually debugged the code, i.e. set a breakpoint and stepped through code line by line, examining the state at each step? I would expect not, because, as far as I can see, the issue should be obvious. If the user enters "4" then that OptionExitBackpack will be called and the message that the program is ending will be displayed. Then what? What have you actually done to make the program exit?
 
Hey NisseMax, try this....

C#:
static void OptionExitBackpack()
        {
            Console.Write("The program is ending. . .");
            Environment.Exit(0);
        } //End OptionExitBackpack

Chris
 
Sure it will work, but you are laying down the foundations of spaghetti.
 
Back
Top Bottom