Double to int & program crashing?

I'm trying to create a payroll program, but it shows this "warning C4244: '=' : conversion from 'double' to 'int', possible loss of data" what should I do?, and also i'm having issues when I enter an integer instead of a decimal or integer instead of a decimal...it crashes, how can I prevent this from happening? when starting the program it will not show the total of the pay rates and shows the incorrect average of the pay rates.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
	int hours_1, hours_2, hours_3, hours_4, hours_5, total_hours, avg_pay_rate, total_pay1, total_pay2, total_pay3, total_pay4, total_pay5;
	double pay_rate1, pay_rate2, pay_rate3, pay_rate4, pay_rate5, total_pay_rate;

	//  hours worked 

	cout << "Hours worked by employee #1 : ";
	cin >> hours_1;
	cout << "Hours worked by employee #2 : ";
	cin >> hours_2;
	cout << "Hours worked by employee #3 : ";
	cin >> hours_3;
	cout << "Hours worked by employee #4 : ";
	cin >> hours_4;
	cout << "Hours worked by employee #5 : ";
	cin >> hours_5;

	//total hours worked 

	total_hours = hours_1 + hours_2 + hours_3 + hours_4 + hours_5;

	//  hourly pay rate for employees

	cout << "Hourly pay rate for employee #1 : ";
	cin >> pay_rate1;
	cout << "Hourly pay rate for employee #2 : ";
	cin >> pay_rate2;
	cout << "Hourly pay rate for employee #3 : ";
	cin >> pay_rate3;
	cout << "Hourly pay rate for employee #4 : ";
	cin >> pay_rate4;
	cout << "Hourly pay rate for employee #5: ";
	cin >> pay_rate5;

	// Average pay rate  

	avg_pay_rate = (pay_rate1 + pay_rate2 + pay_rate3 + pay_rate4 +  pay_rate5 / 5.0;

	//Calculate total Pay 

	total_pay1 = hours_1 * pay_rate1;
	total_pay2 = hours_2 * pay_rate2;
	total_pay3 = hours_3 * pay_rate3;
	total_pay4 = hours_4 * pay_rate4;
	total_pay5 = hours_5 * pay_rate5;

	// Total  pay rates
	total_pay_rate = total_pay1 + total_pay2 + total_pay3 + total_pay4 + total_pay5;

		// the total of all the pay rates
	total_pay_rate = total_pay1 + total_pay2 + total_pay3 + total_pay4 + total_pay5;

	//Display the table

	cout << "\nEmployee#    Hours     Pay Rate    Total Pay\n";
	cout << "===============================================\n";
	
	// Display the rows
	cout << setw(6) << "1" << setw(10) 
		<< pay_rate1 << setw(10) << "$" << total_pay1 << endl;

	 
	cout << setw(6) << "2" << setw(10)
		<< pay_rate2 << setw(10) <<" $" << total_pay2 << endl;
	
	
	cout << setw(6) << "3" << setw(10)
		<< pay_rate3 << setw(10) <<" $" << total_pay3 << endl;
	
	 
	cout << setw(6) << "4" << setw(10)
		<< pay_rate4 << setw(10) <<" $" << total_pay4 << endl;
	
	
	cout << setw(6) << "5" << setw(10)
		 << pay_rate5 << setw(10) <<" $" << total_pay5 << endl;

	
	cout << "Total/Average" << setw(3) << total_hours << setw(12)
		 << total_pay_rate << endl;
	
	system("pause");



}
Last edited on
If you get a compile error, the program is not being built. You can't run a program that hasn't been built, but you can run an old version that was there and didn't get deleted.

Which line number is that error on?

The return type of main must be int

You should use loops instead of copypasting code and declaring excessive variables.
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data what should I do?

Basically stop converting your double values to an int.
avg_pay_rate = (pay_rate1 + pay_rate2 + pay_rate3 + pay_rate4 + pay_rate5 / 5.0;
avg_pay_rate is an int, the calculation is using floating point numbers, hence the warning.

Why are you using an int for all those variables? Do you realize that an int doesn't have fractional parts? Isn't it possible for someone to work a fraction of an hour?

Also this program really looks like it could use arrays instead of all the separate variables like total_pay3. Any time you start putting numeric suffixes on variables it's time to consider arrays. You may even want to consider an array of a structure that holds the information for each employee.
Hi LB,

It shows the warning for lines 42 - 50.



An error is not a warning.

If you want to work with doubles, stop trying to store them in integers.
Topic archived. No new replies allowed.