hi, i need help please...

i need to write a program to calculate the depreciation of equipment passed its useful life using two different methods...

these are the actual requirements for the program:


Program requirements:


write a c++ program to calculate depreciation using the straight-line and sum-of-digits methods.


this program must use at least three functions


depreciation is a decrease in value of some asset over time due to wear and decay. For exampl, suppose that a company purchases a computer system for $200,000 that will serve its needs for a five-year period. After that time, it can be sold for an estimated $50,000. The equipment will have a depreciated by $150,000 during the five-year period.


the input to the program will be the initial cost, the residual value, and the number of years to depreciate, plua a code to indicate whether to use straight-line or sum-of-digits method.


the output of the program will be a series of tables, one for each of the input lines. The program must use an end-of-file loop to get credit.


The program must use file I/O



200000.0 50000.0 5 S // S means straight-lie method
200000.0 50000.0 5 D // D means sum-of-digits method
15000.0 2500.0 10 S
20000.0 2500.0 8 D



for each depreciation table, write the initial cost and the residual value above the table with an appropriate message.
The tables themselves should have column heading for each item. Under the columns, put the year, the depreciation for that year, the accumulated depreciation, and the carrying value. (The carrying value is the initial cost minus the accumulated depreciation.) The output must be formatted. Use a setprecision of 2 for the floating point numbers.



(this is supposed to look like a table but on the post, it doesnt...)

Sum-of-Digits method

Original cost= 200000.00 Residual Value= 50000.00

Current year Current deprec Accum Depreciation Carry Value

1 50000.00 50000.00 150000.00

2 40000.00 ` 90000.00 110000.00

3 30000.00 120000.00 80000.00

4 20000.00 140000.00 60000.00

5 10000.00 130000.00 50000.00


and this is the code i have come up with so far...

any help qill be greatly appreciated....

thank you in advance...

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void inLine (float, float, float, float, float, int, int);
void sumDigits (float, float, float, float, float, float, int, int);

int main()

{   //main starts

    ifstream inputfile;       //input file
    ofstream outputfile;      //output file, created by program

    float cost;               //original cost of equipment
    float junkValue;          //value at end of useful life
    float den;                //denominator
    float dep;                //depreciation
    float depsum;             //accumulative depreciation
    float carry;              //cost of equipment minus accumulative depreciation
    int years;                //useful life
    int counter;              //counts from one to useful life
    char method;              //identifies depreciation method

    inputfile.open("inputfile.txt");       //attempts to open input file

    if (!inputfile)                        //if it fails to open input file...

    {  //if starts

       cout << "Failure to open input file, please check and try again..." << endl;
       //system ("PAUSE");          // i am getting an error here, when i want the system to pause...
       return 1;

    }  //end of if

    outputfile.open("outputfile.txt");      //creates output file

    if (!outputfile)                        //if it fails to create file...

    {  //if starts 2

       cout << "Failure to create output file, please check and try again..." << endl;
       //system ("PAUSE");         //same error here , it gives me an error whe i try to make the system pause....
       return 1;

    }  //end of if2

    inputfile >> cost;

    while (inputfile)

    {     //while loop

    inputfile >> junkValue >> years >> method;

    if (method == 'S' || method == 's')
               sumDigits(cost, junkValue, den, dep, depsum, carry, years, counter);
    else if (method == 'D' || method == 'd')
               inLine(cost, junkValue, dep, depsum, carry, years, counter);
               cout << "Can not determine method to calculate depreciation, please check input file data..." << endl;

    //inputfile >> cost;

    }      //end of while loop


    for(counter=1, counter<= years, counter++)


        outputfile << setw(5)  << counter;
        outputfile << setw(20) << dep;
        outputfile << setw(19) << depsum;
        outputfile << setw(8)  << carry;


    inputfile >> cost;

    return 1;

}   //end of main...

void sumDigits ( float cost, float junkValue, float den, float dep, float depsum, float carry, int years, int counter)

{   //open void sumDigits


    for( counter=years; counter >= 1; counter--)


          depsum= depsum+dep;


     carry = cost - depsum;

}    //close void sumDigits...

void inLine ( float cost, float junkValue, float dep, float depsum, float carry, int years, int counter)

{    //open void inLine

     depsum=depsum + dep;
     carry=cost - depsum;


i know i have many comments,but those comments are cleaned out before i hand it in, it is just form me to keep track of what i am doing, which seems not to be working .....

thank you for the help guys (and ladies)

Recommended Answers

All 3 Replies

>>//system ("PAUSE"); // i am getting an error here, when i want the system to pause...
return 1;

Don't use system() functions unless absolutely necessary. Use something like cin >> ch; or cin.get() or some other istream method to pause the program until user inputs something if want before the program exits. BTW: I'd use exit(1) or exit(EXIT_SUCCESS)instead of return 1.

Instead of this:
inputfile >> cost;
while (inputfile)
    //do something
    inputfile >> cost;

do this 

while(inputfile >> cost)
   //do something

thanks...that looks much cleaner, i will change it to that...

but the whole program is just not doing what i need it to do, and i do not know what i am doing wrong...

You need to say what you expect the program to do vs what it is doing, what it's not doing it should be doing, what error messages you get if it you get any, what lines the error messages apply to, etc.

In addition, either use more descriptive variable names or more descriptive comments. Here's an example of using more descriptive variable names and better comments in an effort calculate a series of values.

//inline deprciation means equal amount of depreciation every year.

yearlyDepreciation = ((initialCost - junkValue) / years);

carryValue = initialCost;

for(int i = 0; i < years; ++i)
    currentValue = carryValue;
    acumulatedDepreciation += yearlyDepreciation;
     carryValue = currentValue - yearlyDepreciation;

     cout << "in year:" << i + 1    
            << "\n \t the current depreciation was " << yearlyDepreciation 
            << "\n \t the accumulated depreciation was " << accumulatedDeprication 
            << "\n \t the carry value was " <<  carryValue << endl << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.