c++ - 如何找到我在 C++ 代码中犯的错误?
问题描述
在输出中,我不断得到 -"dollars" 和 -"cents" 而不是如图所示的正数。
我使用了一个名为储蓄账户的类来设置初始余额、存款和取款。我被要求使用 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;
}
解决方案
你问我如何才能找到我在 C++ 代码中犯的错误?(而不是我在 C++ 代码中犯的错误是什么?)
您的问题的部分答案是,当您第一次开始学习编程时,这尤其适用于小程序,用铅笔和纸跟踪程序的执行。然后,随着您的程序变得更长和更复杂,开始使用调试器观察它们的执行情况。
推荐阅读
- charts - Google BarChart - the horizontal base line is not appearing
- java - 查找文本中的所有字谜并以原始文字打印
- javascript - Angular ng-smart-table delete 确认模态剂量不起作用
- c# - Asp.net core 2.1 到 Asp.net 3.0 升级
- r - 36 核的 R 比 8 核的 R 慢得多
- java - Java 从 SMTP 服务器 * 获取 * 邮件
- terminal - 在 Alacritty 中逐字移动光标
- ios - iOS swift UICollectionView在滚动超过第一个屏幕后失去单元格约束
- java - JTextField setDocument 覆盖文本
- php - 如何在 Laravel 中绕过电子邮件