gpt4 book ai didi

c++ - 方法未更新int

转载 作者:行者123 更新时间:2023-12-03 07:07:05 24 4
gpt4 key购买 nike

#include <iostream>
using namespace std;

class CreditCard {
public:

int balance;
float points;


CreditCard () {
balance = 0;
points = 0;
}

void charge() {
int charge;
cout << "Amount to charge:" << endl;
cin >> charge;
cout << endl;
points = points + (charge/100.);
balance = balance + charge;
return;
}

void payment() {
int payment;
cout << "Amount to pay:" << endl;
cin >> payment;
cout << endl;
balance = balance - payment;
return;
}

void currentBal() {
cout << endl;
cout << "Balance: " << balance << endl;
cout << "Points: " << points << endl;
return;
}

};

int main() {

int userChoice;
bool run = true;

while (run == true) {
cout << "Would you like to:" << endl;
cout << "1) Charge" << endl;
cout << "2) Make a payment" << endl;
cout << "3) See current balance" << endl;
cout << "4) Quit" << endl;
cin >> userChoice;

CreditCard card1;

switch (userChoice) {
case 1:
card1.charge();
break;
case 2:
card1.payment();
break;
case 3:
card1.currentBal();
break;
case 4:
run = false;
break;
}
}

}
我有这个作业来练习方法和类。该程序将打印带有某些选项的菜单。当我选择选项1并设置余额,然后选择选项3来打印余额时,余额保持为0。我已经在计费方法中放入了打印语句,表明余额已更新,但是一旦我调用currentBal方法,余额重新设置为0。

最佳答案

您的CreditCard card1;在while循环中,循环的每次迭代都会创建一张新卡。

关于c++ - 方法未更新int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64705328/

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