title: Why are my account balances showing up as 0 when I run my program?
body: <p>I'm learning about pointers and have been working on an assignment where my program is supposed to ask two people for their account balances, determine which account has the most money in it, prompt the user for the cost of dinner, a movie, and ice cream on a date, and then deduct that amount from the account with the most money. It then shows the updated account balances. For some reason, the balances are showing as $0.00 when I run the program. Can you please help? I've tried adding &amp;'s in a few places, using ** instead of * in my compareBalances function as one person I talked to suggested, and a few other things, but nothing seems to work. Here's my code:</p>
  
  <pre><code>/***********************************************************************
  * Program:
  *    Assignment 33, Pointers
  *    Sister Unsicker, CS124
  * Author:
  *    Lanie Molinar
  * Summary: 
  *    This program asks two people for their bank account balances and the cost
  *    of dinner, a movie, and ice cream on a date. It then deducts the cost 
  *    from the bank account with the most money.
  *
  *    Estimated:  2.0 hrs   
  *    Actual:     0.0 hrs
  *      Please describe briefly what was the most difficult part.
  ************************************************************************/
  
  #include &lt;iostream&gt;
  using namespace std;
  
  /***********************************************************************
  * This function asks two people for their account balances and stores the 
  * information.
  ***********************************************************************/
  void getBalances(float &amp;account1, float &amp;account2)
  {
     cout &lt;&lt; "What is Sam's balance? ";
     cin &gt;&gt; account1;
     cout &lt;&lt; "What is Sue's balance? ";
     cin &gt;&gt; account2;
     return;
  }
  
  /***********************************************************************
  * This function compares the balances in both accounts to determine which is 
  * larger.
  ***********************************************************************/
  void compareBalances(float account1, float account2,  float * pAccount)
  {
     if (account1 &gt; account2)
        pAccount = &amp;account1;
     else
        pAccount = &amp;account2;
     return;
  }
  
  /***********************************************************************
  * This function prompts the user for the cost of the date and then deducts it 
  * from the account with the most money.
  ***********************************************************************/
  void date(float * pAccount)
  {
     float priceDinner;
     float priceMovie;
     float priceIceCream;
     cout &lt;&lt; "Cost of the date:\n"
        &lt;&lt; "\tDinner:";
     cin &gt;&gt; priceDinner;
     cout &lt;&lt; "\tMovie: ";
     cin &gt;&gt; priceMovie;
     cout &lt;&lt; "\tIce cream: ";
     cin &gt;&gt; priceIceCream;
     *pAccount -= priceDinner;
     *pAccount -= (priceDinner * 0.15);
     *pAccount -= priceMovie;
     *pAccount -= priceIceCream;
     return;
  }
  
  /***********************************************************************
  * This function reports the new account balances.
  ***********************************************************************/
  void report(float account1, float account2)
  {
     cout.setf(ios::fixed);
     cout.setf(ios::showpoint);
     cout.precision(2);
     cout &lt;&lt; "Sam's balance: $" &lt;&lt; account1 &lt;&lt; endl;
     cout &lt;&lt; "Sue's balance: $" &lt;&lt; account2 &lt;&lt; endl;
     return;
  }
  
  /**********************************************************************
  *    The main function calls the other functions in the program.
  ***********************************************************************/
  int main()
  {
     float account1;
     float account2;
     float * pAccount;
     getBalances(account1, account2);
     compareBalances(account1, account2, pAccount);
     date(pAccount);
     report(account1, account2);
     return 0;
  }
  </code></pre>
  
  <p>Update: I've made some changes to my code but am now getting an error saying "Segmentation fault" when I run it. It compiles with no problem, and it asks for all the information, but when it should display the account balances, it says this. Here is my updated code:</p>
  
  <pre><code>/***********************************************************************
  * Program:
  *    Assignment 33, Pointers
  *    Sister Unsicker, CS124
  * Author:
  *    Lanie Molinar
  * Summary: 
  *    This program asks two people for their bank account balances and the cost
  *    of dinner, a movie, and ice cream on a date. It then deducts the cost 
  *    from the bank account with the most money.
  *
  *    Estimated:  2.0 hrs   
  *    Actual:     0.0 hrs
  *      Please describe briefly what was the most difficult part.
  ************************************************************************/
  
  #include &lt;iostream&gt;
  using namespace std;
  
  /***********************************************************************
  * This function asks two people for their account balances and stores the 
  * information.
  ***********************************************************************/
  void getBalances(float &amp;account1, float &amp;account2)
  {
     cout &lt;&lt; "What is Sam's balance? ";
     cin &gt;&gt; account1;
     cout &lt;&lt; "What is Sue's balance? ";
     cin &gt;&gt; account2;
     return;
  }
  
  /***********************************************************************
  * This function compares the balances in both accounts to determine which is 
  * larger.
  ***********************************************************************/
  void compareBalances(float &amp;account1, float &amp;account2,  float * pAccount)
  {
     if (account1 &gt; account2)
        pAccount = &amp;account1;
     else
        pAccount = &amp;account2;
     return;
  }
  
  /***********************************************************************
  * This function prompts the user for the cost of the date and then deducts it 
  * from the account with the most money.
  ***********************************************************************/
  void date(float * pAccount)
  {
     float priceDinner;
     float priceMovie;
     float priceIceCream;
     cout &lt;&lt; "Cost of the date:\n"
        &lt;&lt; "\tDinner:";
     cin &gt;&gt; priceDinner;
     cout &lt;&lt; "\tMovie: ";
     cin &gt;&gt; priceMovie;
     cout &lt;&lt; "\tIce cream: ";
     cin &gt;&gt; priceIceCream;
     *pAccount -= priceDinner;
     *pAccount -= (priceDinner * 0.15);
     *pAccount -= priceMovie;
     *pAccount -= priceIceCream;
     return;
  }
  
  /***********************************************************************
  * This function reports the new account balances.
  ***********************************************************************/
  void report(float &amp;account1, float &amp;account2)
  {
     cout.setf(ios::fixed);
     cout.setf(ios::showpoint);
     cout.precision(2);
     cout &lt;&lt; "Sam's balance: $" &lt;&lt; account1 &lt;&lt; endl;
     cout &lt;&lt; "Sue's balance: $" &lt;&lt; account2 &lt;&lt; endl;
     return;
  }
  
  /**********************************************************************
  *    The main function calls the other functions in the program.
  ***********************************************************************/
  int main()
  {
     float account1;
     float account2;
     float * pAccount;
     getBalances(account1, account2);
     compareBalances(account1, account2, pAccount);
     date(pAccount);
     report(account1, account2);
     return 0;
  }
  </code></pre>