I have searched but i can not seem to find an answer to my problem.

I have to write a program that lets the user enter their salary, and calculates their salary with a 5% raise for 3 years.

When I run the program and enter the value for cSalary the program just closes. (This is a console app by the way.)
The compiler I am using is Dev C++.

This is the code I have now, which the compiler is saying is correct:

// Exercise 29 Pg.260 
#include <iostream>
using namespace std;
int main()
{
    double cSalary = 0.0;
    const double raise = cSalary * 0.05;
    double nSalary = cSalary + raise;
    
   cout <<"Enter current salary: ";
   cin >>cSalary;
   
  for(int counter = 0; counter <=3; counter++)
   {        
         cout <<nSalary<<endl;
         cSalary = nSalary;
   }
   
   return 0;
}

Any help is appreciated.

Edited 5 Years Ago by JesseFarmer: n/a

>>When I run the program and enter the value for cSalary the program just closes
You have to put something at the end of main() to prevent that from happening. One way to do that is to add a line cin.get() before main() returns. Programs execute the lines from top down, that is, it first executes line 1, then 2, then 3, etc. That means you can not put the calculations before the value of the variables are known to the program. For example, the value of cSalary is initialized to 0 on the first line, so the value of raise is cSalary * 0.05 which is raise = 0 * 0.05 = 0. Then on the very next line you have nSalary = cSalary. Since the value of cSalary is 0 (from the previous line) the value of nSalary will also be 0.

To fix that problem you have to put the calculations in the order in which you want them executed.

start of loop (do this 3 times)
   display current salary
   increment salary by 5% (e.g. salary = salary + (salary * 0.05);
end of loop

Edited 5 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.