I am working on this program The problem is that when I hit 5 before 4 to quit I
ID: 639780 • Letter: I
Question
I am working on this program The problem is that when I hit 5 before 4 to quit I have to go out than back in. When i start the program I need to hit 1 than 2 than 3 than 5 than 4 for the output. Here is the code so far:
#include
#include
#include
using namespace std;
void printDepreciation(double value, int year, double perYear);
void straightLine(double value, double years);
void doubleDeclining(double value, double years);
void sumOfYears(double value, int years);
int main()
{
double value = 0.0; // Orignal value of the item
int years = 0; // No of years to depreciate
int method = 0; // The method that user choose
cout << "WELCOME TO JOHN'S DEPRECIATION CALCULATOR" << endl;
while (method != 5)
{
cout << " Enter a depreciation method " << "(1-SL 2-DDB 3-SYD 4-Quit): ";
cin >> method;
while (method > 0 && method <= 3 )
{
cout << "Original value: ";
cin >> value;
cout << "Number of years: ";
cin >> years;
cout << endl;
if (method == 1)
{
cout << "Straight-Line Method ";
straightLine(value, years);
}
else if (method == 2)
{
cout << "Double-Declining-Balance Method ";
doubleDeclining(value, years);
}
else if (method == 3)
{
cout << "Sum-of-the-Years'-Digits Method ";
sumOfYears(value, years);
}
cout << " Enter a depreciation method " << "(1-SL 2-DDB 3-SYD 4-Quit): ";
cin >> method;
} // end while
if (method == 4) // put a else in front of if
{
cout << "Seeya. Have a nice day! " << endl;
}
else if (method >= 5)
{
cout << "Invalid entry please Try Again " << endl;
}
} // end while
} // end main
//**************************************************************
// The Function calculate the depreciation and calls the print
// function
void straightLine(double value, double years)
{
double perYear = value / years;
for (int i = 1; i <= years; i++)
{
value -= perYear;
printDepreciation(value, i, perYear); //call print function
} // end for
} // end straightLine function
//*************************************************************
// This function calculates depreciation by
// double declining method and calls the print Function
void doubleDeclining(double value, double years)
{
for (int i = 1; i <= years; i++)
{
double perYear = value * 0.4;
value -= perYear;
printDepreciation(value, i, perYear); // calls for print function
} // end for
} // end while
//******************************************************************
// This function calculates depreciation by SumOfYears
// method and calls the print Funtion
void sumOfYears(double value, int years)
{
int yearsFactor = years;
double startingValue = value;
for (int i = 1; i <= years; --yearsFactor, ++i)
{
double z = 0.0; // variable to add up the years
for (int j = 0; j <= years; j++)
{
z += j;
}
double perYear = (yearsFactor / z) * startingValue;
value -= perYear;
printDepreciation(value, i, perYear); // calls for print funtion
} // end for
} // end sumOfYears
//*******************************************************************
// This function prints out the output
void printDepreciation(double value, int year, double perYear)
{
cout << "End of year" << setw(3) << year << " Depreciation: "
<< setprecision(2) << fixed << setw(7) << perYear
<< " Current value: " << setw(7) << value << endl;
} // end printDepreciation
Explanation / Answer
instead of while (method > 0 && method <= 3 )
if (method > 0 && method <= 3 ) then no need of usage of break
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.