В выводе я получаю - "доллары" и - "центы" вместо положительных значений, как показано на picture.
Я использовал класс с именем сберегательный счет для установки начального баланса, пополнения и снятия. И меня просят использовать 1 объект, который запрашивает ввод у пользователя, и другой, который использует конструктор перегрузки для инициализации доллара и центов.
// Include Section
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class SavingsAccount
{
public:
SavingsAccount();
SavingsAccount(int, int);
void setInitial(int, int);
void setDeposit(int, int);
void setWithdraw(int, int);
void output();
private:
int dollars;
int cents;
};
// Main Function
int main()
{
// object declaration
// Bank 1
SavingsAccount bank1; //has its values set during definition by the user
//Bank 2
SavingsAccount bank2(200, 50); //uses the constructor to store values into member variables
bank2.setDeposit(40, 50);
bank2.setWithdraw(100, 98);
bank2.output();
cout << "\n\n";
// Variable declaration
string repeat;
int d, c;
int choice;
// Prompt for initial balance
cout << "welcome to your savings account! Please fill in the appropriate information.\n\n";
cout << "Initial balance (dollars): ";
cin >> d;
cout << "Initial balance (cents): ";
cin >> c;
bank1.setInitial(d, c);
do
{
cout << "Pick an option: " << endl
<< "1. Deposit money" << endl
<< "2. Withdraw money" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Deposit amount (dollars): ";
cin >> d;
cout << "Deposit amount (cents): ";
cin >> c;
bank1.setDeposit(d, c);
break;
case 2:
cout << "Withdraw amount (dollars): ";
cin >> d;
cout << "Withdraw amount (cents): ";
cin >> c;
bank1.setWithdraw(d, c);
break;
case 3:
break;
default:
while (choice != 1 && choice != 2)
{
cout << "Invalid choice, enter 1 or 2: \n";
cin >> choice;
}
}
// Display output
bank1.output();
// Prompt for continuing
cout << "\nWould you like to keep going? (y or Y for yes)";
cin >> repeat;
}while (repeat == "y" || repeat == "Y");
cout << "End Program.\n\n";
return 0;
}
SavingsAccount::SavingsAccount()
{
dollars = 0;
cents = 0;
}
SavingsAccount::SavingsAccount(int newD, int newC)
{
dollars = newD;
cents = newC;
cout << "Your initial balance is $" << dollars << "." << cents << endl;
}
void SavingsAccount::setInitial(int initialD, int initialC)
{
while (initialC >= 100)
{
initialC = initialC - 100;
initialD++;
}
dollars = initialD;
cents = initialC;
cout << "Your initial balance is $" << dollars << "." << cents << endl;
}
void SavingsAccount::setDeposit(int depD, int depC)
{
while (depC >= 100)
{
depC = depC - 100;
depD++;
}
dollars = depD + dollars;
cents = depC + cents;
cout << "Depositing $" << depD << "." << depC << " to your account...\n";
}
void SavingsAccount::setWithdraw(int withD, int withC)
{
while (withC >= 100)
{
withC = withC - 100;
withD++;
}
if (withD > dollars)
{
cout << "Not enough money to be withdrawn.\n";
}
else
{
dollars = withD - dollars;
cents = withC - cents;
cout << "Withdrawing $" << withD << "." << withC << " from your account...\n";
}
}
void SavingsAccount::output()
{
cout << "dollars = " << dollars << " cents = " << cents << endl;
}