### Functions

Write a program that has a function that takes a salary as argument. If the salary is less than 20000, give a 20% raise. Return the salary. I was not given any example. I need to know how to start it because I know the formula to raise the salary by 20% is <tot = tot + 0.2 * tot> (or whatever variable I choose to use).
Start like this.

 ``123456`` ``````int raiseSalary(int input) { }``````
A starting point:
 ``12345678910111213141516171819`` ``````#include using namespace std; int get_salary(const int salary) { int new_salary = // your calculation here return new_salary; } int main() { int orig_salary = 15000; int new_salary = get_salary(orig_salary); // show the result }``````
 ``123456789101112131415161718192021222324252627282930`` ``````<#include using namespace std; int fun(int sal) { if(sal>20000) { cout<<"INVALID! \n"; } else { if(sal<=20000) sal = sal + 0.2 * sal; } return 0; } int main() { int sal; cout<<"Please enter the salary: \n"; cin>>sal; cout<<"The Final Salary is: "<

This is what I wrote but it is giving me a problem. My output is coming out to be:
 ``12345`` ``````Please enter the salary: 19000 The Final Salary is: 19000 Press any key to continue...``````

This is a new code that I wrote. Is there anyway to modify it for when they enter a salary greater than 20000 to say invalid?

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; float raise(float); int main() { float sal = 0; cout<<"Please Enter The Salary: \n"; cin>>sal; cout<<"\nThe Final Salary Is: \n"<
 ``123456789101112`` ``````float raise(float s) { if(s<20000) { s = s + .20 * s; } else { // say invalid } return s; }``````
Yes, there is. You need to check the value that the user entered, to see if it's greater than 20000. If it is, don't try and calculate the raise, and instead output the message.
 ``1234567891011121314151617181920212223242526272829303132`` ``````<#include using namespace std; int fun(sal) { if(sal >= 20000) { return(sal); } else { //if(sal<=20000) - no need for this test return(sal + (0.2 * sal)); } int main() { int sal; cout << "Please enter the salary: \n"; cin >> sal; sal = fun(sal); cout << "The Final Salary is: " << sal << endl; //or just // cout << "The Final Salary is: " << (fun(sal)) << endl; system("pause"); return 0; }``````

You were calculating the final salary after you printed it.
Your using ints, ints don't have decimal places, you should consider using doubles.
Last edited on