Search code examples
c#functioninteger

Functions in C# - How to get an integer value into a function?


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.");
}

Solution

  • Well, you see, usually, if you'd be a shop owner, yould like to first calculate how much change you need to give back to the client, and only then give him the change. Not the other way around.

    So, change the code

        GetMenuOption(iOption);
        iOption = Convert.ToInt32(Console.ReadLine());
    

    to

        iOption = Convert.ToInt32(Console.ReadLine());
        GetMenuOption(iOption);
    

    and try again.

    Also, the other function are just taking parameters from the outside. They are doing something, but they are not giving anything in return. For example, this function:

    static int FillCrisps(int piFillCrisps, int piTotalNumCrisps)
    

    tries to change the counters, but as it only "reads" the parameters from the outside, whatever it calculates is not visible at the outside variables. Try changing it to

    static int FillCrisps(ref int piFillCrisps, ref int piTotalNumCrisps)
    

    and adjust rest of the code. Now the changes will be visible in the outer variables too.

    On the other hand, this function:

    static void InspectMachine(ref int piTotalNumCrisps)
             //Needs a way of retrieving the total value into it
    

    only READS and PRINTS the values. It does not change them. You do not need the ref here at all. Change it back to:

    static void InspectMachine(int piTotalNumCrisps)
    

    and it will be OK (as long as you correct the other functions too!).

    So, your functions should have signatures of:

    // functions that don't change the data
    static int GetMenuOption(int piOption)
    static void InspectMachine(int piTotalNumCrisps)
    
    // functions that DO change the data
    static int FillCrisps(ref int piFillCrisps, ref int piTotalNumCrisps)
    static int BuyCrisps(ref int piBuyCrisps, ref int piTotalNumCrisps)