循环播放奇怪的地址结果C ++

时间:2014-04-09 21:58:45

标签: c++ loops

我想要让用户输入他们购买的总成本和他们支付的金额,并向用户提供他们的总变化以及他们从五,十,硬币,便士等获得的变化。 ...但由于某种原因,我不能得到更改以显示正确的数量,因为我一直得到这个更改错误(下面的图片),它也保持循环和循环。我究竟做错了什么?

提前感谢您的帮助!

Looping Over & Over

Driver.cpp

#include "MoneyChanger.h" 
#include <string>
#include <iostream>

using namespace std;

int main()
{
    MoneyChanger results;
    int twenties, tens, fives, ones, quarter, dimes, nickels, pennies;
    double purchase, given;
    string again = "yes";

    do{

    cout << "please enter total cost of purchase: $";
    cin >> purchase;

    cout << "\nplease enter amount given: $";
    cin >> given;

        //grabbing the bills and coins
        results.GetBills(&twenties, &tens, &fives, &ones);
        results.GetCoins(&quarter, &dimes, &nickels, &pennies);

        results.setData(purchase, given);

        //results
        cout << "Your change is: $" << results.getTotalChange() << "\n\n";
        cout << twenties << " Twenties" << endl;
        cout << tens << " Tens"  << endl;
        cout << fives << " Fives"  << endl;
        cout << ones << " Ones"  << endl;
        cout << quarter << " Quarters"  << endl;
        cout << dimes << " Dimes"  << endl;
        cout << nickels << " Nickels"  << endl;
        cout << pennies << " Pennies"  << endl;

        cout << "\n\nWould you like to calculate again?\nEnter yes or no\n";
        cin >> again;
    } while (again == "yes");


    cout << "Thank you for using this MoneyChanger!" << endl;

    return 0;
}

MoneyChanger.cpp

#include "MoneyChanger.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 
#include <cstdlib> 

using namespace std;

MoneyChanger::MoneyChanger()
{


}

void MoneyChanger::setData(double pp, double given)
{
    amountP = pp;
    amountG = given;
    CalcChange();
}
void MoneyChanger::CalcChange()
{
    while(totalChange >= 20){totalChange = totalChange-20; twenty++;}
    while(totalChange >= 10){totalChange = totalChange-10; ten++;}
    while(totalChange >= 5){totalChange = totalChange-5; five++;}
    while(totalChange >= 1){totalChange = totalChange-1; one++;}
    while(totalChange >= .25){totalChange = totalChange-.25; quarter++;}
    while(totalChange >= .10){totalChange = totalChange-.10; dime++;}
    while(totalChange >= .05){totalChange = totalChange-.05; nickel++;}
    while(totalChange >= .01){totalChange = totalChange-.01; penny++;}
}
double MoneyChanger::getTotalChange()
{
    totalChange = amountG - amountP;
    return totalChange;
}
void MoneyChanger::GetBills(int *twenties, int *tens, int *fives, int *ones)
{
    *twenties = twenty;
    *tens = ten;
    *fives = five;
    *ones = one;
    CalcChange();
}
void MoneyChanger::GetCoins(int *quarters, int *dimes, int *nickels, int *pennies)
{
    *quarters = quarter;
    *dimes = dime;
    *nickels = nickel;
    *pennies = penny;
    CalcChange();
 }

MoneyChanger.h

#ifndef H_MONEYCHANGER_H
#define H_MONEYCHANGER_H

using namespace std;

class MoneyChanger 
{ 
private: 
    double amountP, amountG, totalChange;
    int twenty, ten, five, one, change;
    int quarter, dime, nickel, penny;
    void CalcChange();
public:
    MoneyChanger();
    void setData(double pp, double given);
    double getTotalChange();
    void GetBills(int *twenties, int *tens, int *fives, int *ones);
    void GetCoins(int *quarters, int *dimes, int *nickels, int *pennies);
};
#endif

2 个答案:

答案 0 :(得分:3)

您没有在构建时初始化您的成员变量。

改变这个:

MoneyChanger::MoneyChanger()
{
}

对此:

MoneyChanger::MoneyChanger()
    : amountP(), amountG(), totalChange(),
      twenty(), ten(), five(), one(), change()
      quarter(), dime(), nickel(), penny()
{
}

要使此类可远程重用,请执行以下操作:

void MoneyChanger::CalcChange()
{
    twenty = ten = five = one = 0;
    quarter = dime = nickel = penny = 0;

    while(totalChange >= 20){totalChange = totalChange-20; twenty++;}
    while(totalChange >= 10){totalChange = totalChange-10; ten++;}
    while(totalChange >= 5){totalChange = totalChange-5; five++;}
    while(totalChange >= 1){totalChange = totalChange-1; one++;}
    while(totalChange >= .25){totalChange = totalChange-.25; quarter++;}
    while(totalChange >= .10){totalChange = totalChange-.10; dime++;}
    while(totalChange >= .05){totalChange = totalChange-.05; nickel++;}
    while(totalChange >= .01){totalChange = totalChange-.01; penny++;}
}

我也发现在CalcChange()函数的 end 上调用Get时没什么意义。如果有的话,它应该在开头。

答案 1 :(得分:-2)

  • 在使用之前,您应该在这里输入所有变量的代码。
  • 使用引用变量比使用指针更安全。
  • 你可以在setData()结束时调用一次CalcChange() *在调用setData之前应该初始化totalChange: 喜欢:

    void MoneyChanger :: setData(double pp,double given) {     amountP = pp;     amountG =给定;     totalChange = amountG - amountP;

    CalcChange();
    

    }