need suggestions and help plz

here the problem i wrote the coding and i would like to get some corrections or any advices that i should change or add

thank you

1. Effective January 1st of each year, Gabriela receives 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. Th e
program also should calculate and display her total salary for the
three years.

a. Create an IPO chart for the problem, and then desk-check the
algorithm using an annual salary of $10,000. (Th e raise amounts
are $500.00, $525.00, and $551.25. Th e total salary is $33,101.25.)


#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 please enclose your code in the appropriate [code] (C++ code goes here) [/code] tags. It will format it in a more viewable way for the rest of the forum, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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;

} 


Secondly, you forgot to add the calculation for the total salary, so you should probably add that in somewhere in the loop so that it displays properly at the end. Third, you can combine a lot of those 'cout' lines into single lines, it's pretty flexible about that. E.g.:

1
2
3
		cout << fixed << setprecision(2);
		salary = salary*(1+RATE);
		cout << "Year " << numYears << ": $" << salary << endl;


For the for loop itself, I'd suggest declaring the number of years as a constant just like the RATE:

 
const unsigned int numYears = 3; // The number of years to calculate for. Unsigned (always positive) 


And then change the for loop to use a generic loop value with that as a condition, something like this:

1
2
3
4
5
int i;
for (i = 1; i <= numYears; i ++)
{
	[..]
}


This has the advantage of making it more clear what's going on in the code. It also allows the number of years to be easily changed just by changing a single variable.

On an unrelated note, system() is something that you should avoid using if you ever write something that you want to share widely, as it's extremely operating system specific, and there are better and more portable ways to do almost anything you can do with system() by using native C++. Don't worry about it too much at this stage though, but keep it in mind as you progress in ability.
Last edited on
Topic archived. No new replies allowed.