Please help with fstream function

The code will not open the file and receive the ID numbers. What am i doing wrong?
My assignment is to get the ID numbers from a file and find the hours they worked and then calculate their wages
I am getting one value for the ID number and it's not even in the file; it's a random value.

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
 #include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;

int main()
{
	const int ID = 10;
	int emp[ID];
	
	ifstream reader("employee_ids.txt");
	if (reader.is_open())
	{
		for (int i = 0; i < ID; ++i)
		{
			reader >> emp[i + 1];

			cout << emp[i + 1] << endl;
		}
	}
	else 
	{
		cout << "Error in opening the file" << endl;
	}
	reader.close();
    double hours[ID];
	double pay_rate[ID]; 
	double wages[ID]; //declare another array to store wages
	for (int i = 0; i < ID; i++)
	{
		do
		{
			cout << "Enter the hours for employee " << (i + 1) <<" ID = "<< emp[i+1]<<": ";
			cin >> hours[i];
			if (hours[i] < 0)
			{
				cout << "Re-enter" << endl;
				cin >> hours[i]; //when u ask to re enter, re read in hours[i] using cin
			}
		}while (hours[i] < 0);
	}

	for (int i = 0; i < ID; i++)
	{
		do
		{
			cout << "Enter the Pay rate for employee " << (i + 1) <<" ID: "<< emp[i+1]<<": ";

			cin >> pay_rate[i];
			if (pay_rate[i] < 15)
			{
				cout << "Re-enter" << endl;
				cin >> pay_rate[i]; //when u ask to re enter, re read in pay_rate[i] using cin
			}
		} while (pay_rate[i] < 15);
	}

	for (int i = 0; i < ID; i++)
	{
		double total = hours[i] * pay_rate[i];
		wages[i] = total; //store the wage for each employee in respective entry in wages array
	}
	//use setw to setwidth of each column while displaying employee id and wages
	cout<<setw(15)<<"Employee ID"<<setw(10)<<"Wages"<<endl;

	for (int i = 0; i < ID; i++)
	{
		cout<<setw(15)<<emp[i]<<setw(10)<<wages[i]<<endl;
	}
	system("pause");
	return 0;
}
Lines 17,19,34,48: You're making out of bounds references to your array.
emp is declared with 10 occurrences. Those occurrences are 0-9. You're referencing 1-10.

but I have 10 id numbers in the file.
I tried rewriting it but without the file and it worked.
This time I need the ID's from the file.

here is the code without the file that's working.

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
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
const int ID = 10;
int emp[ID] = { 5658845, 7634875, 3762945, 1763523, 7243988, 1764354, 5231545, 4653211,
8342334, 7342998 };
double hours[ID];
double pay_rate[ID];  
double wages[ID];        //declare another array to store wages
for (int i = 0; i < ID; i++)
{
    do
    {
        cout << "Enter the hours for employee " << (i + 1) <<" ID = "<< emp[ID]<<": ";
        cin >> hours[i];
        if (hours[i] < 0)
        {
                cout << "Re-enter" << endl;
                cin >> hours[i];             //when u ask to re enter, re read in hours[i] using cin
        }
        }while (hours[i] < 0);
}

for (int i = 0; i < ID; i++)
{
    do
    {
        cout << "Enter the Pay rate for employee " << (i + 1) <<" ID: "<< emp[ID]<<": ";
        cin >> pay_rate[i];
        if (pay_rate[i] < 15)
        {
            cout << "Re-enter" << endl;
            cin >> pay_rate[i];        //when u ask to re enter, re read in pay_rate[i] using cin
        }
    } while (pay_rate[i] < 15);
}

for (int i = 0; i < ID; i++)
{
    double total = hours[i] * pay_rate[i];
    wages[i] = total;         //store the wage for each employee in respective entry in wages array
}
//use setw to setwidth of each column while displaying employee id and wages
cout<<setw(15)<<"Employee ID"<<setw(10)<<"Wages"<<endl;

for (int i = 0; i < ID; i++)
{
    cout<<setw(15)<<emp[i]<<setw(10)<<wages[i]<<endl;
}
system("pause");

return 0;
}
Never-mind, now I understood what you meant.
I fixed it -- Thank you for the help.
Topic archived. No new replies allowed.