类不起作用的折旧函数
Depreciation functions with classes not working
我正在编写一个程序,该程序在一个名为Asset的类中具有单折旧和双折旧功能。我在双重折旧函数以及如何在课堂上实现它方面遇到了麻烦。此外,对于如何验证用户输入(即,当被问及字符时,没有否定、没有零、只有一个字符),有什么建议吗?这是我的主文件、类文件和类头文件。
当我运行这个程序并尝试使用双倍折旧功能时,它只使用直线折旧。它忽略了资产类别中的双重函数。为什么?此外,在我的main中,当有人输入无效输入时,它会执行if循环,但它会继续执行main的其余部分,我如何让它回到do-while循环的顶部?
// Depreciation.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include "Asset.h"
#include <limits>
using namespace std;
using namespace System;
char choice;
int main()
{
double c;
double s;
double l;
int y = 0;
bool wrongdata = false;
//normal input task; welcome message, input of 3 fields and output features - with full data validation
cout << "Welcome to the Depreciation calculator!"<<endl;
do{
cout << "Please enter an Asset Cost: " << endl;
cin >> c;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout<<"Please input a correct value" << endl;
wrongdata = true;
}
if (c == 0)
{
cout << "Thanks for using my program!" << endl;
break;
}
cout<< "Please enter the Salvage Value: " << endl;
cin >> s;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Please input a correct value" << endl;
wrongdata = true;
}
if (s == 0)
{
cout << "Thanks for using my program!"<<endl;
break;
}
cout << "Please enter the Asset life (in years): " << endl;
cin >> l;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
cout << "Please input a correct value" << endl;
wrongdata = true;
}
if (l == 0)
{
cout << "Thanks for using my program!" << endl;
break;
}
Asset a = Asset(c, s, l);
cout << "The annual depreciation is equal to " << a.getAnnualDep() << " for Straight-Line and n" << a.getDDDep(y) << " for one year of Double Declining" << endl;
cout << "Would you like to see a depreciation schedule for Straight-Line, DDL, or Neither? (S/D/N)" << endl;
cin >> choice;
choice = toupper(choice);
if (choice = 'S')
{
cout << "Year Start Value Depreciation End Value" << endl;
for (int i = 1; i <= a.getorigLife(); i++)
{
cout << i << "tt" << a.getBegBal(i) << "tt" << a.getAnnualDep() << "tt" << a.getEndBal(i) << endl;
}
}
else if (choice = 'D')
{
cout << "Year Start Value Depreciation End Value" << endl;
for (int j = 1; j <= a.getorigLife(); j++)
{
cout << j << "tt" << a.getDDBBal(j) << "tt" << a.getDDDep(y) << "tt" << a.getDDEBal(j) << endl;
}
}
else if (choice = 'N')
{
cout << "Thanks for using my program" << endl;
break;
}
else
{
cout << "Could not understand your input";
wrongdata = true;
}
} while (wrongdata);
system("pause");
return 0;
}
//asset class
#include "stdafx.h"
#include "Asset.h"
Asset::Asset(double c, double s, int L)
{
ocost = c;
osalv = s;
oLife = L;
anndep = (c - s) / L;
double SLrate = (100.0 / L)/100;
DDanndep = (2 * SLrate) * c;
double DDrate = (2 * SLrate);
//declare arrays for starting and ending values
bbal = new double[oLife];
ebal = new double[oLife];
DDebal = new double[oLife];
DDbbal = new double [oLife];
//calculate straight line depreciation
bbal[0] = ocost;
DDbbal[0] = ocost;
for (int i = 0; i < oLife; i++)
{
if (i > 0)
{
bbal[i] = ebal[i - 1];
}
ebal[i] = bbal[i] - anndep;
}
//calculate double depreciation
for (int j = 0; j < oLife; j++)
{
if (j > 0)
{
DDbbal[j] = DDebal[j - 1];
}
DDebal[j] = DDbbal[j] - (DDbbal[j] * DDrate);
DDbbal = DDebal;
}
}
double Asset::getAnnualDep()
{
return anndep;
}
double Asset::getOrigCost()
{
return ocost;
}
double Asset::getOrigSalvage()
{
return osalv;
}
int Asset::getorigLife()
{
return oLife;
}
double Asset::getBegBal(int y)
{
return bbal[y - 1];
}
double Asset::getEndBal(int y)
{
return ebal[y - 1];
}
double Asset::getDDDep(int y)
{
return DDanndep;
}
double Asset::getDDBBal(int y)
{
return DDbbal[y - 1];
}
double Asset::getDDEBal(int y)
{
return DDebal[y - 1];
}
Asset::~Asset()
{
}
//class header
#pragma once
class Asset
{
public:
Asset(double Cost, double Salvage, int Life); //constructor
~Asset(void); //destructor
double getOrigCost(), getOrigSalvage(), getAnnualDep();
double getBegBal(int year), getEndBal(int year), getDDBBal(int year), getDDEBal(int year), getDDDep(int year);
int getorigLife();
char choice;
private:
double ocost, osalv, anndep, DDanndep;
int oLife;
double* bbal;
double* ebal;
double*DDebal;
double*DDbbal;
};
=
是赋值,==
是比较。
在你这样的if语句中:
if (choice = 'S')
您正在将"S"分配给choice
,然后检查它是否为非零。
更改为:
if (choice == 'S')
相关文章:
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么我的数组双精度函数不起作用?
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ Arduino - 随机函数不起作用
- 为什么我的数组或函数不起作用?
- 从类中的另一个文件请求函数不起作用
- 双链表堆栈删除函数不起作用
- 将 cmake 代码段转换为函数 - 不起作用
- 为什么我的从base64解码的函数不起作用?
- 为什么当我们使用等于'='符号比较器函数时,c ++的内置排序函数不起作用?
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 子类化 STL 容器:范围构造函数不起作用
- 带有此指针的模板类多重继承构造函数不起作用?
- 以下打印树函数不起作用为什么?
- 为什么内存集函数不起作用?
- 调用填充我的主体数组的函数不起作用
- 带有自定义数字的阶乘函数不起作用
- 如何QDialog raise()函数不起作用
- Lambda 表达式闭包函数不起作用
- C++:在没有 lvalue 的情况下调用时复制构造函数不起作用