2017-11-10 7 views
-3

私はポインターについて学んでおり、私のプログラムは2人の人に勘定残高を尋ね、最も多くの勘定があるアカウントを決定し、ユーザーに夕食費、映画、そしてアイスクリームを日付の上に置いて、その金額を最も多く払っている口座から差し引く。更新された勘定残高が表示されます。何らかの理由で、私がプログラムを実行すると天びんは$ 0.00として表示されます。あなたは助けてもらえますか?私はいくつかの場所に&を追加しようとしました。*私のcompareBalances関数で*の代わりに**を使用しました。私は提案した人といくつかのことを話しましたが、何も動作していないようです。ここに私のコードは次のとおりです。私のプログラムを実行すると、私の勘定残高が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:  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; 
} 

アップデート:私は私のコードにいくつかの変更を加えましたが、私はそれを実行したときに、今「セグメンテーションフォールトを」というエラーを取得しています。それは問題なくコンパイルされ、すべての情報を尋ねますが、勘定残高を表示する必要がある場合は、これが表示されます。ここに私の更新されたコードがあります:

/*********************************************************************** 
* 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」は期待どおりに動作しません。 *を値*で渡すと、コピーされ、ローカルコピーにのみ割り当てられます。ポインタを参照渡しします。 –

+0

ポインタが必要な問題の説明に何も表示されません。 – juanchopanza

+0

割り当てにはポインタを使用すると書かれていますが、ポインタを使用する例がありますが、関数は1つしかないのであまり複雑ではありません。ポインタを参照渡ししようとします。 – user65209

答えて

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; 
} 
関連する問題