开发者

C++ Programming - Calculating recurrent increases

开发者 https://www.devze.com 2023-02-19 00:35 出处:网络
Effective January 1st of each year, Gabby recieves a 5% raise on her previous year\'s salary. She wants a program that calculates and displays the amount of her annual raises for the next three years.

Effective January 1st of each year, Gabby recieves a 5% raise on her previous year's salary. She wants a program that calculates and displays the amount of her annual raises for the next three years. The program also should calculate and display her total salary for the three years.

I have to test the program and this is what i get but when i desk check it comes out wrong.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const double RATE = .05;
double salary = 0.0; 
double raise = 0.0;
double totalSalary = 0.0; 

cout << "Enter the salary:";
c开发者_运维问答in >> salary;

for(int counter = 0; counter <=3; counter++)
{ 
cout <<salary<<endl;
raise = (salary * 0.05);


}

return 0;
} //end of main function


You're not adding the raise to the salary:

salary += raise;


This one is a little more accurate. It will calculate with decimals without rounding and post the raise for each year, then add how much the total of her raises will be over three years. Also, some notes are added so you know what was going on in the code.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//declare variables
double salary = 0.0;
double totalSalary = 0.0;
const double RATE = 0.05;

//input
cout << "Enter the salary: ";
cin >> salary;
cout << endl;

//loop for years and calculation
for(int numYears = 1; numYears <= 3; numYears ++)
{
cout << fixed;
cout << setprecision(2);
salary = salary*(1+RATE);
cout << "Year " << numYears;
cout << ": $" << salary << endl;
//end for
}

cout << "The total salary over three years is $" << totalSalary << endl;
cout << endl;

system("pause");
return 0;

}


First of all, you declare a constant called RATE (which should really be at the top of the program rather than in your main function) but don't bother to use this in your calculation. Instead you use the hard-coded value of 0.05.

Second, you're not adding the calculation to the salary variable. You can use salary += raise or salary *= (1.0 + RATE).

Third, you're not doing anything with the totalSalary variable at all.

Your code should look something like this:

#include <iostream>
using namespace std;

const double RATE = 0.05;

int main()
{
    double salary = 0.0; 
    double totalSalary = 0.0; 

    cout << "Enter the salary:" << endl;
    cin >> salary;

    for(int counter = 0; counter <=3; counter++)
    { 
        cout << salary << endl;
        salary *= (1.0 + RATE);\
        totalSalary += salary;
    }

    cout << "The total salary is " << totalSalary << endl;

    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消