繁体   English   中英

循环和奇怪的地址结果C ++

[英]Looping & Strange Address Results C++

我假设要求用户输入购买的总成本和所支付的金额,并向用户提供总零钱以及他们从五,五,十,一角钱,几美分等获得的零钱。但由于某些原因,由于我不断收到此更改错误(如下图所示),所以我无法使更改显示正确的金额(也如下图所示),并且它也不断循环播放。 我究竟做错了什么?

在此先感谢您的帮助!

循环遍历

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

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

更改此:

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++;}
}

我还看不到在Get函数末尾调用CalcChange()的意义。 如果有的话,应该在开始时。

  • 您应该在使用所有变量之前在此处初始化输入代码。
  • 使用对变量的引用比使用指针更安全。
  • 您可以在setData()的末尾调用CalcChange()一次*必须在调用setData之前初始化totalChange:

    无效MoneyChanger :: setData(double pp,double给定){amountP = pp; amountG =给定; totalChange =数量G-数量P;

     CalcChange(); 

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM