### Accumulating values in a for loop in order to add them.

I'm obviously new to c++ and am having trouble understanding for loops. For this problem, I have to create a program that allows a user to input a base salary and will calculate a 5% raise for 3 years. Then display the total salary after 3 years.
This is what I have so far.

 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int main() { //declare variables double baseSalary = 0.0; int year = 0; double raise = 0.0; double salary= 0.0; //calculate 5% raise per year for 3 years for (year = 1; year <= 3; year +=1) { cout << "Enter your base salary: \$"; cin >> baseSalary; raise = baseSalary * .05; cout << "Raise: \$"<< raise << endl; salary = baseSalary + raise; cout <<"Total yearly salary: \$"<< salary <

I have to keep entering the yearly salary but my professor wants us to only enter it once and have the loop do all of the work. I really just need to figure out how to display the total after 3 years (which I think is done outside of the for loop).
Thanks
Place the entering the salary outside the loop. What is the problem?
You only need 1 double variable, not the 3 you have.

 ``1234567891011121314`` ``````for (year = 1; year <= 3; year +=1) { cout << "Enter your base salary: \$"; // Place these two lines outside the loop cin >> baseSalary; // Change this to salary raise = baseSalary * .05; cout << "Raise: \$"<< raise << endl; // Just remove both of these lines salary = baseSalary + raise; // Rewrite this as salary *= 1.05 // This translates to salary = salary * 1.05, which is the same as // salary = salary + salary * .05, which is essentially what you had before cout <<"Total yearly salary: \$"<< salary <

Do what I said above, and it should work for you.
Thanks that worked perfectly. Now all I need to do is show the raise separately from the total salary for each of the 3 years as well as the overall total for the 3 years. The total salary should add up to \$33,101.25. The raises should display \$500, \$525, and \$551.25.

 ``12345678910111213141516171819202122232425`` ``````#include using namespace std; int main() { //declare variables int year = 0; double raise = 0.0; double salary= 0.0; cout << "Enter your base salary: \$"; cin >> salary; //calculate 5% raise per year for 3 years for (year = 1; year <= 3; year +=1) { salary *= 1.05; cout <<"Total yearly salary: \$"<< salary <
Just put `raise = salary * 0.05;` before you multiply salary by 1.05. And then you can output the raise along with the total salary.
Hey thanks, this is actually coming together. One last question though. After the for loop I still can't get it to show the total for everything.

 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int main() { //declare variables int year = 0; double raise = 0.0; double salary= 0.0; cout << "Enter your base salary: \$"; cin >> salary; //calculate 5% raise per year for 3 years for (year = 1; year <= 3; year +=1) { raise = salary * 0.05; salary *= 1.05; cout <<"Raise: \$" << raise <

Last edited on
Topic archived. No new replies allowed.