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.
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
usingnamespace std;
int main()
{
constint 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.
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
constint 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;
}