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 &'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:
/***********************************************************************
* 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 <iostream>
using namespace std;
/***********************************************************************
* This function asks two people for their account balances and stores the
* information.
***********************************************************************/
void getBalances(float &account1, float &account2)
{
cout << "What is Sam's balance? ";
cin >> account1;
cout << "What is Sue's balance? ";
cin >> 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 > account2)
pAccount = &account1;
else
pAccount = &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 << "Cost of the date:\n"
<< "\tDinner:";
cin >> priceDinner;
cout << "\tMovie: ";
cin >> priceMovie;
cout << "\tIce cream: ";
cin >> 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 << "Sam's balance: $" << account1 << endl;
cout << "Sue's balance: $" << account2 << 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;
}
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:
/***********************************************************************
* 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 <iostream>
using namespace std;
/***********************************************************************
* This function asks two people for their account balances and stores the
* information.
***********************************************************************/
void getBalances(float &account1, float &account2)
{
cout << "What is Sam's balance? ";
cin >> account1;
cout << "What is Sue's balance? ";
cin >> 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 > account2)
pAccount = &account1;
else
pAccount = &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 << "Cost of the date:\n"
<< "\tDinner:";
cin >> priceDinner;
cout << "\tMovie: ";
cin >> priceMovie;
cout << "\tIce cream: ";
cin >> 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 << "Sam's balance: $" << account1 << endl;
cout << "Sue's balance: $" << account2 << 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;
}