The output is not correct

Ok, I am creating this program that calculates and displays the amount of annual raises for the next three years, using rates of 2%. 3%, 4% and 5%. The program should end when the user enters a sentinel value as the salary.

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
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>

using namespace std;
int main ()
{
     //variables
      double salary = 1.0;
      double RATE = .03;

     // enter salary
      cout << "Salary (-1 to stop): ";
      cin >> salary;

    // display raises with 2 decimal places
     cout << fixed << setprecision(2);

    // Begin loop
     while (salary = 0.0)
      {
       cout << "Salary (negative number to stop): ";
       cin >> salary;

       cout << "Annual Raises for next three years: ";

     //While loop to calculate annual raises
      while (RATE < .06)
      {
       // display annual raise amount
       cout << RATE * 100 << "% Raise: ";
       cout << salary * RATE << endl;

       // Update salary value and annual raise rate
       salary += (salary * RATE);
       RATE += .01;
      } //end of inner loop
    } //end of outer loop
   return 0;
} //end of file      




It is allowing me to enter a salary but when I push enter it says "Salary (negative number to stop): That is not what I need.
Last edited on
You mean to use < instead of = on line 19?
Ok, I tried the = sign and after I input the salary it goes to Press any key to continue.... What am I doing wrong?
Now the code is not allowing me to input a negative number to stop. After it outputs the next 3 years. I input -1 it comes up as "Annual Raises for next three years:" and on the next line it shows me "Press any key to continue....."

while (salary > 0.0)
You may also try
1
2
3
4
5
6
#include<cstdlib>
while(true){
   if(salary < 0){
      exit(0);
   }
}


If your problem is solved , please mark it as solved using the button above , thanks.
Last edited on
Raman009 and hacker804,

I did the while (salary > 0.0)

I am not sure what the
1
2
3
4
5
6
#include<cstdlib>
while (true) {
if (salary < 0) {
exit (0);
}
}


Not sure how I can incorporate that in the problem.
Think about your logic: on line 13, you get a salary from the user, and then on 22, you get it again without ever processing the salary you got on line 13! Probably not what you want. Move the logic to get salary from the user from line 22 to the very end of the while loop.

Also, you use a variable RATE in your logic. By convention, a variable that is all caps like RATE is constant or a #define, but you are changing it throughout the program, and never re-initializing it back to the starting value (0.03).

Initially you say you are calculating salaries for 3 years, given percentages 2, 3, 4, and 5%. Isn't that 4 years of raises though?

And to elaborate on why Peter suggested you change the operation from "=" to "<" - when you write salary = 0.0, you aren't testing IF salary is 0, you are assigning the value '0.0' TO salary. To compare, you need to write salary == 0.0.
Last edited on
You could also do like :
1
2
3
4
5
6
7
  while(salary !=  -1){
      cout<<"Enter salary";
      cin>>salary ;

     //process salary
 }
//line after loop 


The code will not stop until you enter the value of salary as -1.
When you enter the salary as -1 , it will come out of loop and execute the line after loop. If nothing is written there , the program will simply exit.

As far as problem is concerned ,do you want to find the final salary after three years when the raise for the first year is 2% , 2nd year is 3% , 3rd year is 4% and so on ? or Have I misunderstood the problem ?

It seems a bit like compound interest problem to me where the salary may be taken as principal , time as 3 years and the rate for each year as 2%,3% and 4% and we have to find amount or compound interest.

You don't actually need to get salary outside the loop. Just initialize it with any value NOT EQUAL TO -1. and you are good to go.
I am trying to get the program to end after inputting (-1).

I inserted a // reset variables and now the code looks 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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
	#include <iomanip>
	
	using namespace std;
	int main ()
	{
	    // variables
	    double salary = 1.0;
	    double RATE = .03;
	
	
	    // display raises with 2 decimal places
	    cout << fixed << setprecision(2);
	
	    //begin loop 
	    while (salary != -1) 
	    {
	        // enter salary
	        cout << "Salary (-1 to stop): "; 
	        cin >> salary;

	        cout << "Annual Raises for next three years: " << endl;
	
	        // while loop to calculate annual raises
			 while (RATE < .06) 
	         {
				 
	            // display annual raise amount
	            cout << RATE * 100 << "% raise: ";
	            cout << salary * RATE << endl;
	
	            // update salary value and annual raise rate.
	            salary += (salary * RATE);
	            RATE += .01;
	
	         } //end of inner loop
			 

			 // reset variables
			 RATE = .03;

	    }   // end of outer loop
	        return 0;
	}   // end of file




When I input a negative zero it gives me years of negative numbers. HELP!
Last edited on
I found the problem and I fixed it. My question is solved. Thanks for everyones help.
Topic archived. No new replies allowed.