我正在学习指针,并且一直致力于一项任务,我的计划应该向两个人询问他们的帐户余额,确定哪个帐户的资金最多,提示用户支付晚餐费用,电影和日期上的冰淇淋,然后从钱最多的帐户中扣除该金额。然后显示更新的帐户余额。出于某种原因,当我运行程序时,余额显示为$ 0.00。你能帮忙吗?我曾尝试在几个地方添加&'s,在我的compareBalances函数中使用**代替*作为我建议的一个人,以及其他一些东西,但似乎没有任何效果。这是我的代码:
/***********************************************************************
* 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;
}
更新:我已对代码进行了一些更改,但现在收到的错误是&#34;分段错误&#34;当我运行它。它编译没有问题,并且它要求所有信息,但是当它应该显示帐户余额时,它会说明这一点。这是我更新的代码:
/***********************************************************************
* 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;
}
答案 0 :(得分:0)
我最终在导师的帮助下弄明白这一点。不幸的是,这并不像我想在我的学校找到导师那么容易,因为大多数人都是忙碌的学生,所以很难找到他们可以提供帮助的时间。这是我的代码:
/***********************************************************************
* 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;
}