-1

So I am trying to pass in one variable, the money variable, and break it into two variables. I am stuck and trying to figure out how to pass back two values when I only passed in one. I am supposed to get a double value of money from the user and split it into two int values. So for example I get a value like 3.45 and split it up and print out the message, "There is 3 dollars and 45 cents in $3.45". I understand pass by reference, but I am just trying to figure out like I said how to get two variables back. And I can ONLY pass in the money variable to the method I know my program is not right. Just looking for some ideas and explanations on how to do this. Thanks

 using System;

 static class Program
 {
      //Declare any constansts
      const int ONE_HUNDRED = 100;

     static void Main()
     {
         //Declare local variables
         double money = 0;

         //Ask the user to input a money value
         Console.WriteLine("Please enter a money value. (ex. 2.95)");

         //Store the value in a variable
          money = double.Parse(Console.ReadLine());

          //Take the variable and call the SplitMoney method
          SplitMoney(ref money);

          //Display the message
          Console.WriteLine("There are {0:d} and {1:d} cents in ${0:f2}", money, dollars cents);

          Console.ReadLine();
     }//End Main()

      //Split Money Method
      //Purpose: To split the money into dollars and cents
      //Parameters: One double passed by reference
      //Returns: Nothing
      static void SplitMoney(ref double money)
      {
            money = (int)(money * ONE_HUNDRED);
            int dollars = (int)(money / ONE_HUNDRED);
            int cents = (int)(money % ONE_HUNDRED);
      }
   }//End class Program
  • Duplicate http://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c question covers many possible ways to return multiple values - feel free to pick what you like or opn new question if way you need is not covered (return `Tuple`, return custom type, use `out`/`ref` to return each result) – Alexei Levenkov Jun 28 '14 at 00:13
  • An array is often the easiest: `static int[] SplitMoney(decimal money) { return new[] {(int)(money / 100),(int)(money % 100)}; }` – Reactgular Jun 28 '14 at 00:14

2 Answers2

3

For your question:

public class SplittedMoney
{
    public int Dollars { get; set; }
    public int Cents { get; set; }
}

You return that. That would be the easiest way.

A somewhat better way may be:

public struct SplittedMoney
{
    public readonly int Dollars;
    public readonly int Cents;

    public SplittedMoney(int dollars, int cents)
    {
        Dollars = dollars;
        Cents = cents;
    }
}

This is better, as a splitted dollar is still a value, so using an immutable struct is the way to go here.

Now, don't use doubles in anything that requires precise decimal calculations. Use decimals instead as they're designed for that task. A double can lose precision is base 10, as it is stored and manipulated in base 2. A decimal is designed to preserve precision in base 10.


Here's a rewrite of SplitMoney using the above struct:

  static SplittedMoney SplitMoney(decimal money)
  {
        var totalCents = (int)(money * 100);
        int dollars = (int)(totalCents / 100);
        int cents = (int)(totalCents % 100);
        return new SplittedMoney(dollars, cents);
  }
Jonathan Wilson
  • 4,138
  • 1
  • 24
  • 36
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
0

Well, SplitMoney is currently returning 0 values. You could continue to mutate money to contain only the dollar value, and then actually return cents from the method.

  static int SplitMoney(ref double money)
  {
        money = (int)(money * ONE_HUNDRED);
        int dollars = (int)(money / ONE_HUNDRED);
        int cents = (int)(money % ONE_HUNDRED);
        return cents;
  }

Then when you use it:

var moneyCents = SplitMoney(ref money);
Console.WriteLine("money = {0:f2}.{1:f2}", money, moneyCents);

Better yet, you can return a tuple containing both dollars and cents:

  static Tuple<int,int> SplitMoney(double money)
  {
        var totalCents = (int)(money * ONE_HUNDRED);
        int dollars = (int)(money / ONE_HUNDRED);
        int cents = (int)(money % ONE_HUNDRED);
        return Tuple.Create(dollars,cents);
  }

Which would be used as

var splitMoney = SplitMoney(money);
Console.WriteLine("money = {0:f2}.{1:f2} from {2:d}",
    splitMoney.Item1, splitMoney.Item2, money);
Jonathan Wilson
  • 4,138
  • 1
  • 24
  • 36
  • 2
    I should note that `SplitMoney` should probably be rewritten to be a pure function (does not mutate a value outside its scope) that returns a `SplittedMoney` object, as Lucas described in his answer. My solution just requires a small change to your code. – Jonathan Wilson Jun 28 '14 at 00:09
  • Actually your thought won't work. My statement ahs to be Console.Writeline("There are {0:d} dollars and {1:d} cents in {3:f2)", money, dollars, cents); – Charlie Barber Jun 28 '14 at 00:13
  • In that case, the answer with `SplittedMoney` is definitely the way to go. The problem with this approach is that the original money value is no longer available since you mutated that variable in your method. – Jonathan Wilson Jun 28 '14 at 00:16
  • Do you have any other ideas? I am suppose to use doubles and there are no structs used – Charlie Barber Jun 28 '14 at 00:21
  • I just added an implementation of `SplitMoney` that returns a 2-Tuple – Jonathan Wilson Jun 28 '14 at 00:33