简体   繁体   English

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

[英]Looping & Strange Address Results C++

I'm suppose to ask the user to enter the total cost of their purchase and the amount they paid for and provide the user their total change and how much change they get from ones, fives, tens, dimes, pennies etc.... but for some reason I cannot get the change to show the correct amount as I keep getting this change error (pic below) and it also keeps looping and looping. 我假设要求用户输入购买的总成本和所支付的金额,并向用户提供总零钱以及他们从五,五,十,一角钱,几美分等获得的零钱。但由于某些原因,由于我不断收到此更改错误(如下图所示),所以我无法使更改显示正确的金额(也如下图所示),并且它也不断循环播放。 What am I doing wrong? 我究竟做错了什么?

Thanks in advance for your help! 在此先感谢您的帮助!

循环遍历

Driver.cpp 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 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 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

You're not initializing your member variables on construction. 您没有在构造时初始化成员变量。

Change this: 更改此:

MoneyChanger::MoneyChanger()
{
}

To this: 对此:

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

To make this class remotely reusable, do the following as well: 要使该类可以远程重用,请执行以下操作:

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

I also see little point in invoking CalcChange() at the end of your Get functions. 我还看不到在Get函数末尾调用CalcChange()的意义。 It should be at the beginning if anything. 如果有的话,应该在开始时。

  • You should initialize enter code here`e all your variables before using them. 您应该在使用所有变量之前在此处初始化输入代码。
  • It's safer to use reference to variables than pointer. 使用对变量的引用比使用指针更安全。
  • you could call CalcChange() once at the end of setData() *totalChange should be initialized before calling setData : like : 您可以在setData()的末尾调用CalcChange()一次*必须在调用setData之前初始化totalChange:

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

     CalcChange(); 

    } }

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

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