Reputation: 65
#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;
}
}
}
I have this assignment to practice methods and classes. The program prints a menu with some options. When I choose option 1 and set the balance and then choose option 3 to print the balance, the balance stays at 0. I have put print statements in the charge method that show that balance is updated, but once I call the currentBal method, the balance gets set back to 0.
Upvotes: 1
Views: 61
Reputation: 1397
Your CreditCard card1;
is in the while loop, a new card gets created every iteration of the loop.
Upvotes: 6