Basically I'm trying to get an integer value into a function, I'm making a simple program for a vending machine, where I have an option to "Inspect machine" which counts the number of crisps in the machine, Basically the problem I have is that when I come to see how many is actually in the machine, it says the amount is 0.. How would I fix this up so that it allows me to actually view how many crisps are currently in the machine?
Here is my code so far:
static void Main(string[] args)
{
//Declare variables
int iOption = 0; //used to store users menu option
int iFillCrisps = 0; //used to store the amount of crisps the user wants to add to the machine
int iBuyCrisps = 0; //used to store the amount of crisps the user wants to buy
int iTotalNumCrisps = 0; //used to show the total number of crisps in the machine
//Menu
while (iOption != 4) //Program continously loops until user types "4" which is the exit key
{
GetMenuOption(iOption);
iOption = Convert.ToInt32(Console.ReadLine());
//Process menu
if (iOption == 1)
{
FillCrisps(iFillCrisps, iTotalNumCrisps);
}
else if (iOption == 2)
{
BuyCrisps(iBuyCrisps, iTotalNumCrisps);
}
else if (iOption == 3)
{
InspectMachine(ref iTotalNumCrisps);
}
else if (iOption == 4)
{
Console.WriteLine("Exit");
}
}
}
static int GetMenuOption(int piOption)
{
Console.WriteLine("Vending machine");
Console.WriteLine();
Console.WriteLine("Please choose an option");
Console.WriteLine("1: Fill machine with crisps");
Console.WriteLine("2: Buy Crisps");
Console.WriteLine("3: Inspect Machine");
Console.WriteLine("4: Exit");
return piOption;
}
static int FillCrisps(int piFillCrisps, int piTotalNumCrisps)
{
Console.WriteLine("Fill machine with crisps");
Console.WriteLine("How many crisps would you like to add to the machine?");
piFillCrisps = Convert.ToInt32(Console.ReadLine());
piTotalNumCrisps = piFillCrisps + piTotalNumCrisps;
Console.WriteLine("You are adding " + piFillCrisps + " packs of crisps to them machine");
Console.WriteLine("There are now " + piTotalNumCrisps + " packs of crisps in the machine");
return piTotalNumCrisps;
}
static int BuyCrisps(int piBuyCrisps, int piTotalNumCrisps)
{
Console.WriteLine("Buy Crisps");
Console.WriteLine("How many crisps would you like to buy?");
piBuyCrisps = Convert.ToInt32(Console.ReadLine());
piTotalNumCrisps = piTotalNumCrisps - piBuyCrisps;
Console.WriteLine("You are buying " + piBuyCrisps + " crisps");
Console.WriteLine("There are now " + piTotalNumCrisps + " packs of crisps in the machine");
return piTotalNumCrisps;
}
static void InspectMachine(ref int piTotalNumCrisps) //Needs a way of retrieving the total value into it
{
Console.WriteLine("Inspect Machine");
Console.WriteLine("There are currently " + piTotalNumCrisps + " crisps in the machine.");
}
FillCrispsshould probably beiTotalNumCrisps = FillCrisps(iFillCrisps, iTotalNumCrisps).