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.

1
2
3
4
5
6
int raiseSalary(int input)
{



}
A starting point:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

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
}
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
<#include<iostream>
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: "<<sal<<endl;
    fun(sal);
      
      system("pause");
      return 0;
      
}


This is what I wrote but it is giving me a problem. My output is coming out to be:
1
2
3
4
5
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
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"<<raise(sal)<<endl;
         
      system("pause");
      return 0;     
}

 float raise(float s)
 {
       if(s<20000)
       s = s + .20 * s;
       return s;
       }
1
2
3
4
5
6
7
8
9
10
11
12
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.
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
<#include<iostream>
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
Topic archived. No new replies allowed.