-3

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;
}
  • 4
    `compareBalances` doesn't work as you expect. You pass the pointer *by value* meaning it is copied and you assign to only the local copy. Pass the pointer by reference. – Some programmer dude Nov 10 '17 at 05:34
  • I can't see anything in the problem description that requires pointers. – juanchopanza Nov 10 '17 at 05:37
  • The assignment says to use pointers, and I'm given an example to follow that uses them but is less complex with only one function. I'll try to pass the pointer by reference. – Lanie Molinar Nov 10 '17 at 05:42
  • @user65209 In addition to what was said about `compareBalances` - if you only passed `pAccount` by reference - dereferencing it would exhibit undefined behavior due to it storing dangling pointers (to local function variables outside of its scope). – Algirdas Preidžius Nov 10 '17 at 05:44
  • If you initialized `pAccount` to `nullptr` in `main()`, you will see that right after the call to `compareBalances`, `pAccount` will still be `nullptr`. Now you would have an idea that what you did wouldn't work, thus go to plan B (which would be to figure out why `pAccount` didn't change and make corrections / read up more on passing pointers). – PaulMcKenzie Nov 10 '17 at 05:47
  • I don't really understand. This is just a very basic intro to software development course, and I have no clue what nullptr means. I tried changing the code for calling compareBalances to " compareBalances(account1, account2, &pAccount);", but I'm now getting errors. – Lanie Molinar Nov 10 '17 at 05:51
  • I've read through the examples and textbook several times, and I'm still not sure what I could be doing wrong. – Lanie Molinar Nov 10 '17 at 05:52
  • @user65209 "_but I'm now getting errors._" And I already explained the reason for it, before you even wrote this comment. – Algirdas Preidžius Nov 10 '17 at 05:54
  • 2
    @user65209 -- `pAccount = nullptr;` This is basic C++ syntax. I don't understand why this was not taught to you -- you're learning pointers. Get a new teacher if this was not taught. Also, the point I am making with my first post is that you should have seen that `pAccount` did not change, yet you intended it to be changed in the call to `compareBalances`. Now ask yourself "why didn't my pAccount pointer change values after the call to the function?" – PaulMcKenzie Nov 10 '17 at 05:56
  • What does "segmentation fault" mean? I think I'm on my way to fixing this, but I keep getting that error. – Lanie Molinar Nov 10 '17 at 06:33
  • 1
    @user65209 Consider reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Segmentation fault is one of many possible manifestations of undefined behavior somewhere in your code. Typically associated with illegal memory access. Step through your code with a debugger, to find it. – Algirdas Preidžius Nov 10 '17 at 06:40
  • Regarding the changes you made, only post the *changed* functions (with a note about the changes you made), no need to post it all again, especially the file header comment. And I said that you should pass ***the pointer*** by reference, not the other arguments. – Some programmer dude Nov 10 '17 at 06:50
  • As an addendum to my previous comment, the other arguments *also* needs to be passed by reference. Or you would use pointer to local variable that go out of scope and disappear once the function returns, leaving you with a pointer to a non-existing variable. So you're *partially* fixed the problem. – Some programmer dude Nov 10 '17 at 06:59
  • Okay, I got rid of the &'s for all other arguments and am using pass-by-pointer, which I believe is the same as what you suggested, so my code now has:compareBalances(account1, account2, &pAccount); date(&pAccount); report(account1, account2); – Lanie Molinar Nov 10 '17 at 07:01
  • That last comment sent before I was ready. I'm now getting errors: – Lanie Molinar Nov 10 '17 at 07:05
  • a33.cpp: In function int main(): a33.cpp:91:49: error: cannot convert float** to float* for argument 3 to void compareBalances(float, float, float*) compareBalances(account1, account2, &pAccount); ^ a33.cpp:92:18: error: cannot convert float** to float* for argument 1 to void date(float*) date(&pAccount); – Lanie Molinar Nov 10 '17 at 07:06
  • Stack overflow isn't a substitution for textbooks, we can neither be a tutorial nor a debugging service. Ask only about **specific problems** – Passer By Nov 10 '17 at 07:15

1 Answers1

0

I eventually figured this out with help from a tutor. Unfortunately, it's not as easy as I would like to get tutors at my school since most are busy students as well, so it's hard to find times when they can help. 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:     6.0 hrs
*      I had a lot of trouble getting the account balances to show up right 
*      at the end of running the program. It took me a long time to figure out
*      what I was doing wrong. I was finally able to fix it by using 
*      pass-by-reference in some locations and pass-by-pointer in others.
************************************************************************/

#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 -= 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;
}