我正在编写一个名为Asset的类的具有单折旧和双折旧功能的程序。我在双重折旧功能以及如何将其实现到班级上遇到麻烦。另外,关于如何使用户输入有效的任何建议(即,没有否定,没有零,并且在询问字符时只有一个字符)?这是我的主文件,类文件和类头文件。
当我运行此程序并尝试使用双折旧功能时,它仅使用直线一。它忽略资产类中的double函数。这是为什么?另外,在我的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 << "\t\t" << a.getBegBal(i) << "\t\t" << a.getAnnualDep() << "\t\t" << 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 << "\t\t" << a.getDDBBal(j) << "\t\t" << a.getDDDep(y) << "\t\t" << 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')
您正在将分配给
choice
,然后检查它是否为非零。更改为:
if (choice == 'S')