Please HELP with depreciation calculator \'int main\' coding using C++!!! COUT i
ID: 3858687 • Letter: P
Question
Please HELP with depreciation calculator 'int main' coding using C++!!!
COUT in BOLD _ My coding is below the instructions, I need help with 'int main' in Depreciation.cpp
Below is what I would like the calculator to do:
Modify your Asset class so that it is capable of calculating ‘Double Declining’ depreciation as well as straight line. This will require you to have an additional set of return methods: getDDDep(y), getDDBBal(y), and getDDEBal(y) for returning the double declining depreciation for any year ‘y’. The Annual Depreciation return function will now be overloaded as getAnnualDep(y) since the double-declining depreciation changes from year-to-year. The main control program will also, of course, need to ask the user which type of depreciation schedule is desired (Straight-line or Double
Declining). The above example would look like this:
Welcome to the depreciation calculator!
Please enter the Asset Cost (0 to quit): 30000
Please enter the Salvage Value: 5000
Please enter the Asset Life (in years): 5
The annual depreciation allowance on a $30,000.00 asset with a salvage value of $5,000.00 and a life of 5 years = $5,000.00 per year under straight line or $12,000.00 first year depreciation under double-declining.
Would you like to see a complete schedule for Straight-Line, DDL, or Neither? (S/D/N)? D
Year Start Value Depreciation End Value
1 30,000.00 12,000.00 18,000.00
2 18,000.00 7,200.00 10,800.00
3 10,800.00 5,000.00 5,800.00
4 6,480.00 800.00 5,000.00
5 0.00 0.00 5,000.00
Please enter the Asset Cost (0 to quit): 0
Thanks for using the depreciation calculator!
Notes on Double Declining depreciation:
In this alternate method, you still use purchase price and salvage value, but the depreciation calculated each year is at twice the rate of straight line (but not exactly twice the amount): the catch here is that the calculation takes the straight-line rate doubles it, and then applies that rate to the previous-year’s ending balance. Typically, at some point the double-declining method will produce yearly depreciation amounts below straight line – but instead of continuing with the declining method, at that point the straight line amount is used instead. Using our example, we first see that the straight line method = 20% per year (100% divided by 5 years); the double-declining method would thus be 40% per year – but based on the changing ending asset values in each year. Thus, a ‘pure’ double-declining schedule would be:
Year Start Value Depreciation End Value
1 30,000 12,000 (=30,000 * .4) 18,000
2 18,000 7,200 (=18,000 * .4) 10,800
3 10,800 4,320 (=10,800 * .4) 6,480
4 6,480 1,480 ** 5,000
5 --no depreciation allowed--
Note that in year 4, the calculated depreciation would have been 2592 (6,480 * .4), but only 1480 was allowed because that brought the asset value down to its salvage value level. Even though the asset is used in year 5, there is no taxable depreciation expense allowed. Note again, however, that in year 3 the calculated depreciation is actually less than the straight-line amount would be for that year (4,320 vs. 5,000); at that point, depreciation is usually switched to straight-line, so the actual schedule calculated should have been:
Year Start Value Depreciation End Value
1 30,000 12,000 (=30,000 * .4) 18,000
2 18,000 7,200 (=18,000 * .4) 10,800
3 10,800 5,000 5,800 (5000 > [10,800 * .4])
4 6,480 800 5,000
5 --no depreciation allowed--
Again, the salvage value is as low as the ending balance is permitted to go.
MY current Code using C++ in VB
Asset.h.
#pragma once
class Asset
{
public:
Asset(double Cost, double Salvage, int life);
~Asset(void);
double getOrigCost(), getOrigSalvage(), getAnnualDep();
double getBegBal(int year), getEndBal(int year);
int getOrigLife();
private:
double ocost, osalv, anndep;
int olife;
double* bbal;
double* ebal;
};
Asset.cpp
#include "stdafx.h"
#include "Asset.h"
Asset::Asset(double c, double s, int l)
{
ocost = c;
osalv = s;
olife = l;
anndep = (c - s) / l;
//declare arrays for starting and ending values
bbal = new double[olife];
ebal = new double[olife];
//calculate straight line depreciation values
bbal[0] = ocost;
for (int i = 0; i < olife; i++)
{
if (i > 0)
{
bbal[i] = ebal[i - 1];
}
ebal[i] = bbal[i] - anndep;
}
}
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];
}
Asset::~Asset(void)
{
}
Depreciation.cpp
// Depreciation.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include "Asset.h"
#include <string>
using namespace std;
using namespace System;
int choice;
bool choose(string);
bool quit;
double getVal(string);
int main()
{
char choice;
cout << "Welcome to the depreciation calculator! " << endl;
do
{
cout << " Please enter the Asset Cost (0 to quit): " << endl;
cin >> choice;
if (!cin.good())
{
cin.clear();
cin.ignore(1000, ' ');
choice = '?';
} while (choice != 0);
cout << "Thanks for using the depreciation calculator!" << endl << endl;
system("Pause");
return 0;
double cost = getVal(" ");
double salvage = getVal(" ");
Asset a = Asset(30000.00, 5000.00, 5);
cout << " The annual depreciation allowance on a ‘ ‘" << a.getOrigCost() << "asset with a salvage value of ‘ ‘" << a.getOrigSalvage() << " and a life of" << a.getOrigLife() << " y = ‘ ‘" << a.getAnnualDep() << "nper year under straight-line." << endl << endl;
if (choose( "Would you like to see a complete schedule for Straight-Line, DDL, or Neither? (S/D/N)? "))
{
cout << "Year | Start Value | Depreciaton | End Value" << endl;
for (int i = 1; i < a.getOrigLife() + 1; i++)
{
cout << i << " " << a.getBegBal(i) << " " << a.getAnnualDep() << " " << a.getEndBal(i) << endl;
}
}
} while (choose("Please enter the Asset Cost (0 to quit: "));
cout << "Thanks for using the depreciation calculator!" << endl << endl;
system("Pause");
return 0;
}
bool choose(string message)
{
char choice;
cin.ignore(1000, ' ');
cout << message;
cin >> choice;
if (!cin.good())
{
cin.clear();
cin.ignore(1000, ' ');
return false;
}
else if (choice == 'y' || choice == 'Y')
{
return true;
}
return false;
}
double getVal(string message)
{
double value;
do
{
cout << message;
cin >> value;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
cout << "Your data was bad so I could not decipher your input as a decimal value." << endl;
value = -1;
}
else if (value < 1)
{
cout << "Please enter a positive value." << endl;
}
} while (value < 1);
return value;
}
Explanation / Answer
Answer:
Note: User given code is modified.
File Name: Asset.h
#pragma once
class Asset
{
//Access specifier
public:
//Constructor
Asset(double Cost, double Salvage, int life);
//Destructor
~Asset(void);
//Get the Asset values
double getOrigCost(), getOrigSalvage(), getAnnualDep();
int getOrigLife();
//Include the methods to get the values of the straight line approach
double getBegBal(int year), getEndBal(int year);
//Include the methods to get the values of the double declining approach
double getDDDep(int year);
double getDDBBal(int year);
double getDDEBal(int year);
private:
//Declaration of variables
double ocost, osalv, anndep;
int olife;
double* bbal;
double* ebal;
//Declaration of variables for the double declining approach
double* ddbbal;
double* ddebal;
double* dddep;
};
File Name: Asset.cpp
//#include "stdafx.h"
#include "Asset.h"
#include <iostream>
using namespace std;
Asset::Asset(double c, double s, int l)
{
ocost = c;
osalv = s;
olife = l;
anndep = (c - s) / l;
//declare arrays for starting and ending values
bbal = new double[olife];
ebal = new double[olife];
//array declaration for Double declining
ddbbal = new double[olife];
ddebal = new double[olife];
dddep = new double[olife];
//calculate straight line depreciation values
bbal[0] = ocost;
for (int i = 0; i < olife; i++)
{
if (i > 0)
{
bbal[i] = ebal[i - 1];
}
ebal[i] = bbal[i] - anndep;
}
//Calculating begining balance, ending balance and depreciation values for double declining
ddbbal[0]=ocost;
dddep[0]=ddbbal[0]*0.4;
for(int i=0;i<olife;i++)
{
//From second year onwards
if(i>0 && ddebal[i-1]!=5000)
{
ddbbal[i]=ddbbal[i-1]-ddbbal[i-1]*0.4;
dddep[i]=ddbbal[i]*0.4;
}
//Check end balance
else if(i>0 && ddebal[i-1]==5000)
{
ddbbal[i]=0;
dddep[i]=0;
}
//Check depreciation values
if(dddep[i]<osalv &&dddep[i]!=0)
{
dddep[i]=osalv;
}
//Check previous depreciation values
if(dddep[i-1]==osalv)
{
dddep[i]=ddebal[i-1]-dddep[i-1];
}
//Check previous and current depreciation values
if(dddep[i-1]!=osalv && dddep[i]!=0)
//Find end balance
ddebal[i]=ddbbal[i]-dddep[i];
//Check end balance
if(ddebal[i]<osalv)
{
ddebal[i]=osalv;
if(dddep[i]!=0 && dddep[i-1]!=osalv)
dddep[i]=ddbbal[i]-ddebal[i];
}
}
}
//Define method to get the annual depreciation for straight line
double Asset::getAnnualDep()
{
//Return
return anndep;
}
//Get asset cost
double Asset::getOrigCost()
{
return ocost;
}
//Get salvage value
double Asset::getOrigSalvage()
{
return osalv;
}
//Get life time
int Asset::getOrigLife()
{
return olife;
}
//Get straight line beginning balance for year y
double Asset::getBegBal(int y)
{
return bbal[y-1];
}
//Get straight line end balance for year y
double Asset::getEndBal(int y)
{
return ebal[y-1];
}
//Get depreciation value for double declining method for year y
double Asset::getDDDep(int y)
{
return dddep[y-1];
}
//Get beginning value for double declining method for year y
double Asset::getDDBBal(int y)
{
return ddbbal[y-1];
}
//Get end balance for double declining method for year y
double Asset::getDDEBal(int y)
{
return ddebal[y-1];
}
//Destructor
Asset::~Asset(void)
{
}
File Name: Depreciation.cpp
// Depreciation.cpp : main project file.
//#include "stdafx.h"
#include <iostream>
#include "Asset.h"
#include <string>
using namespace std;
//Declaring global variables
int choice;
//Declare method
int choose(string);
bool quit;
//Declare method
double getVal(string);
//main
int main()
{
char choice;
cout << "Welcome to the depreciation calculator! " << endl;
//Loop
do
{
//Get asset cost
cout<<"Please enter the Asset Cost (0 to quit):";
double cost;
cin>>cost;
//If cost is 0
if(cost==0)
//Exit
break;
//Get asset salvage value
double salvage = getVal("Please enter the Salvage Value: ");
//Get the year
int year=getVal("Please enter the Asset Life (in years):");
//Create asset
Asset a = Asset(cost, salvage, year);
//print straight line approach calculation
cout << " The annual depreciation allowance on a " << a.getOrigCost() << " asset with a salvage value of " << a.getOrigSalvage() << " and a life of " << a.getOrigLife() << " y = $" << a.getAnnualDep() << " per year under straight-line." << endl << endl;
int gtval=choose( "Would you like to see a complete schedule for Straight-Line, DDL, or Neither? (S/D/N)? ");
//Check user choice
if ((gtval==1) || (gtval==2))
{
//If straight line approach
if(gtval==1)
{
cout << "Year | Start Value | Depreciaton | End Value" << endl;
for (int i = 1; i < a.getOrigLife() + 1; i++)
{
cout << i << " " << a.getBegBal(i) << " " << a.getAnnualDep() << " " << a.getEndBal(i) << endl;
}
}
//If double declining approach
if(gtval==2)
{
cout << "Year | Start Value | Depreciaton | End Value" << endl;
for (int i = 1; i < a.getOrigLife() + 1; i++)
{
cout << i << " " << a.getDDBBal(i) << " " << a.getDDDep(i) << " " << a.getDDEBal(i) << endl;
}
}
}
} while (1);
cout << "Thanks for using the depreciation calculator!" << endl << endl;
system("Pause");
return 0;
}
//Get approach selection
int choose(string message)
{
char choice;
cin.ignore(1000, ' ');
cout << message;
cin >> choice;
if (!cin.good())
{
cin.clear();
cin.ignore(1000, ' ');
return false;
}
else if (choice == 'S' || choice == 's')
{
return 1;
}
else if (choice == 'D' || choice == 'D')
{
return 2;
}
return -1;
}
//Method return a value
double getVal(string message)
{
double value;
do
{
cout << message;
cin >> value;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
cout << "Your data was bad so I could not decipher your input as a decimal value." << endl;
value = -1;
}
else if (value < 1)
{
cout << "Please enter a positive value." << endl;
}
} while (value < 1);
return value;
}
Sample output:
Welcome to the depreciation calculator!
Please enter the Asset Cost (0 to quit):30000
Please enter the Salvage Value: 5000
Please enter the Asset Life (in years):5
The annual depreciation allowance on a 30000 asset
with a salvage value of 5000
and a life of 5 y = $5000 per year under straight-line.
Would you like to see a complete schedule for Straight-Line, DDL, or Neither? (S/D/N)? D
Year | Start Value | Depreciaton | End Value
1 30000 12000 18000
2 18000 7200 10800
3 10800 5000 5800
4 6480 800 5000
5 0 0 5000
Please enter the Asset Cost (0 to quit):30000
Please enter the Salvage Value: 5000
Please enter the Asset Life (in years):5
The annual depreciation allowance on a 30000 asset
with a salvage value of 5000
and a life of 5 y = $5000 per year under straight-line.
Would you like to see a complete schedule for Straight-Line, DDL, or Neither? (S/D/N)? S
Year | Start Value | Depreciaton | End Value
1 30000 5000 25000
2 25000 5000 20000
3 20000 5000 15000
4 15000 5000 10000
5 10000 5000 5000
Please enter the Asset Cost (0 to quit):0
Thanks for using the depreciation calculator!
Press any key to continue . . .