This is the code that I have: THE PROBLEM that I\'m having is DURING the while l
ID: 3623846 • Letter: T
Question
This is the code that I have:
THE PROBLEM that I'm having is DURING the while loop, if I press -99 it still continues the loop.
If I enter -99 at the very beginning, it works and says good bye, but let's say i put in numbers like $600 and 5 years, the calculator works, and then I want to exit and I enter -99, it still continues the loop.
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
// Welcome User
cout << " " << endl;
cout << "Welcome to calculation of the asset depreciation costs"
<< endl;
cout << "------------------------------------------------------"
<< endl;
cout << " " << endl;
// Ask user for information about amount and # of years
double amount; // Initial cost of the asset
cout << "Enter cost of the asset or -99 to end the program: ";
cin >> amount;
// Execute program unless user enters -99 to exit
while(amount != -99)
{
int numYears; // Number of years to use calculator
cout << "Enter the expected life time of the asset: ";
cin >> numYears;
cout << " " << endl;
// Call the straight line and sum of years methods
straightLine(amount, numYears);
sumOfYears(amount, numYears);
}
cout << "Good bye!" << endl;
return 0;
}
and these are my straightLine and sumOfYears functions
// Straight Line Method
void straightLine(double amount, int numYears)
{
// The method for straight line
double depreciation = amount / numYears;
// Sets up chart when called upon
cout << " Year - Straight - Line Depreciation"
<< " --------------------------------------- ";
cout << fixed << showpoint << right << setprecision(3);
// For loop for numbers entered and sets up in the chart
for(int year = 1; year <= numYears; year++)
cout << setw(3) << year << setw(25) << depreciation << endl;
}
void sumOfYears(double amount, int numYears)
{
// Sets up chart for method when called upon
cout << " Year - Sum Of Years Depreciation"
<< " ---------------------------------------" << endl;
// Adds up numbers from 1 to number entered
double yearSum = (numYears / 2) + (1 + numYears);
double depreciation;
cout << fixed << showpoint << right << setprecision(3);
// For loop for sum method and sets up chart
for(int year = 1; year <= numYears; year++)
{
depreciation = (numYears - year +1) * amount / yearSum;
cout << setw(3) << year << setw(25) << depreciation << endl;
}
}
Is it something to do with the way that I wrote the straight line and sum of years methods?
Please heeelp!!!!
Explanation / Answer
I think what happened is that you didn't read into amount again, so once you've set your value, it continues to compare the initial value to -99. I do this ALL the time im while loops. Just include a statement towards the end of the stuff in your while loop to make sure you write a new value to amount.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.