类不起作用的折旧函数

Depreciation functions with classes not working

本文关键字:函数 不起作用      更新时间:2023-10-16

我正在编写一个程序,该程序在一个名为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;

};
请注意,在c++中,=是赋值,==是比较。

在你这样的if语句中:

if (choice = 'S')

您正在将"S"分配给choice,然后检查它是否为非零。

更改为:

if (choice == 'S')