Problem Returning Calculated Value

I'm trying to get through an assignment and I'm a little lost on the two parts. The homework assignment is asking us to determine pay increase for employee's based on ID number. The user inputs the employee's ID, name, and current salary. If the user's ID is even, their salary receives a 5% increase, and if it is odd, their salary receives a 3% increase.

The program then outputs the employee's ID, name, current salary, monetary value of salary increase, and their updated salary, then the program asks to repeat, and stores the data in the process, until the user inputs the employee ID as negative, or if the user command to repeat as anything but 'y'. In that case, the program outputs the total number of employees getting 3% increases, the monetary sum of all 3% increases, the total number of employees getting 5% increases, and the monetary sum of all 5% increases.

I ran into a roadblock. I got the function calls down, but when I run the code, o displayed for the salary increase or their new total salary. As you can see from my code, I tried returning the salary increase and the new total, to no avail. Am I going about it the wrong way?

My default function calls (for user input as well as the line of code that describes what the program does):
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Compiler directives
#include <iostream>		
#include <string>
#include <iomanip>
using namespace std;

// Function prototypes
void instruct(string);
long inputdata_integer(int, int, string);	
float inputdata_float(float, float, string);
char inputdata_char(string);
string inputdata_string(string);

void instruct(string whatitdoes)
{

	// Post:	Instructions on how to use the program will be displayed 
	cout << "This program: " << whatitdoes << endl << endl;
} // end instruct

long inputdata_integer(int minim, int maxim, string messg)
{
long goback = minim - 1;

		while ( goback < minim || goback > maxim)   //  While loop to check input validity
		 {
		//2.0	Get input from the user
			cout << endl << messg << ": ";
			cin>> goback;
	
		//2.2	Is input between minim and maxim
			if ( goback < minim )
				cout << "input less than " << minim << endl;
			if ( goback > maxim )
				cout << "input greater than " << maxim << endl;

		}
	return goback;
}

float inputdata_float(float minim, float maxim, string messg)
{
float goback = minim - 1.;

		while ( goback < minim || goback > maxim)   //  While loop to check input validity
		 {
		//2.0	Get input from the user
			cout << endl << messg << ": ";
			cin>> goback;
	
		//2.2	Is input between minim and maxim
			if ( goback < minim )
				cout << "input less than " << minim << endl;
			if ( goback > maxim )
				cout << "input greater than " << maxim << endl;

		}
	return goback;
}
char inputdata_char(string messg)
{
char getchar;
			cout << endl << messg << ": ";
			cin>> getchar;

	return getchar;
}
string inputdata_string(string messg)
{
string getstring;
			cout << endl << messg << ": ";
			cin>> getstring;

	return getstring;
}


The main program (consists only of function calls per the assignment requirement):
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
48
49
50
51
52
53
54
55
56
57
// Compiler directives
#include <iostream>				
#include <iomanip>
#include <string>
using namespace std;

// Function prototypes
void instruct(string);				
long inputdata_integer(int, int, string);	// Function to get input
float inputdata_float(float, float, string);
char inputdata_char(string);
string inputdata_string(string);
float percent_increase(float, int, float, float);
void display_individual(int, string, float, float, float);

int main()	
{
// Declaration statements
char repeat = 'y';				// Input - to repeat
int number;					// Input - employee ID number
string name;
float current;
float percentage;
float increase;
float total;
//Program Inputs

	
	// 1.0	Display instructions to the program user
	instruct("Calculates employee salary increase based on user input.");

    while ( repeat == 'y' )        // While loop to repeat entire input
    {
		//2.0	Get input from the user
		//2.1	Prompt user to enter ID number
		number = inputdata_integer(1, 1000000, "Please enter the employee's ID number");

		//	2.2	Prompt user to enter employee name
		name = inputdata_string("Please enter the employee's full name");
		
		//	2.3	Prompt user to enter current salary
		current = inputdata_float(10000, 1000000, "Please enter the employee's current salary");	

		//3.0	Calculate answers
		percentage = percent_increase(current, number, increase, total);
		
		//4.0	Display results to the user
		display_individual(number, name, current, increase, total);
		
		
		cout << endl << "Type y to repeat: ";
		cin >> repeat;
	}

	return 0;				// indicate that the program ended successfully

}   // end of main 


Function calls specifically made for this program (and the main area of my struggle):
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
// Compiler directives
#include <iostream>		// for cin, cout, endl
#include <string>
#include <iomanip>
using namespace std;

float percent_increase(float salary, int id, float added, float updated)
{
	float current = salary;
	int number = id;
	float increase = added;
	float total = updated;
	
	
	if (id % 2 == 0) {// if employee ID is even
    updated = salary * 1.05; // 5% increase
    added = salary * .05; }
	else
    updated = salary * 1.03;
	added = salary * .03;	
	
	return updated;
	return added;
}

void display_individual(int id, string employee, float salary, float added, float updated)
{
		
	cout << "Employee identification number: " << id << endl;
	cout << "Employee Name: " << employee << endl;
	cout << "Current Salary = $" << salary << endl;
	cout << "Salary Increase = $" << added << endl;
	cout << "New Salary = $" << updated << endl;
}
You're missing braces around the two statements right after else if you want them to run together as part of that condition.

The function will end after the first return statement - - it's not going to return a second value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float percent_increase(float salary, int id, float added, float updated)
{
	float current = salary;
	int number = id;
	float increase = added;
	float total = updated;
	
	
	if (id % 2 == 0) {// if employee ID is even
    updated = salary * 1.05; // 5% increase
    added = salary * .05; }
	else
    updated = salary * 1.03;
	added = salary * .03;	
	
	return updated;
	return added;
}



edit:

percentage = percent_increase(current, number, increase, total);

Why are you sending increase and total here? They're uninitialized values. And I could be missing something, but I don't see that you use the value in percentage anywhere else.

Last edited on
Yeah, I just realized that I did not use percentage correctly. As for "increase" and "total", I was under the impression that I had to declare them in that function call in order to utilize them in my "display_individual" function call.
If you want the function to update values of variables declared in the main function so you have an accurate value for a subsequent function, I'd probably pass the variable by reference using the & operator.

You don't need to declare all these local variables in the function to duplicate the values you've sent in.
1
2
3
4
float current = salary;
int number = id;
float increase = added;
float total = updated;
Last edited on
Topic archived. No new replies allowed.