gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:57:29 26 4
gpt4 key购买 nike

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

预先感谢您的帮助!

Looping Over & Over

驱动.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() 没什么意义。如果有的话,它应该在开头。

关于c++ - 循环和奇怪的地址结果 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com