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!
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
}
}