What am I supposed to do with these arrays?

Here's what I'm supposed to do:

"Your program should use the following one-dimensional arrays.

empId: An array of long integers to hold employee identification numbers. Assume there will be no more than 20 identification numbers, but your code should check that the file doesn't enter anymore. If an attempt is made to enter 21, process only the first 20.

hours: An array of doubles to hold the number of hours worked by each employee. Fractional hours are possible.

payRate: an array of doublesnto hold each employee's hourly pay rate.

wages: an array to hold each employee's gross wages."


Here's what I have so far:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


int main() {

const int SIZE=6;
int empId[SIZE];
double hours[SIZE];
double payRate[SIZE];
double wages[SIZE];

cout << fixed << showpoint << setprecision(2);

string inFileName;


ifstream inFile;
inFile.open(inFileName);



return 0;
}


//I'm having a really hard time with what I'm supposed to do in the first array.

//Here's my input:
5658845 40 8
4520125 25.25 9.25
7895122 30 10.50
8777541 10 12
8451277 50 10.20
1302850 35.20 7.50
-1

If you're going to correct me, please don't add anything else to the compiler like vectors or what not. I just need those four types.
Last edited on
you need to loop until you read 20 or reach end of file, and inside the loop, read into the array..

infile >> empid[I] ;
infile >> hours[I];
.. etc
and presumably calculate
wages[I] = rate[I]*hours[I];




So like this?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


int main() {

cout << fixed << showpoint << setprecision(2);

string inFileName;


ifstream inFile;
inFile.open(inFileName);

for (int I=0, i <=20; i++)
{
infile >> empId[I] ;
}
infile >> hours[I];
infile>> payrate[I];

wages[I] = rate[I]*hours[I];

return 0;
}

Sorry I'm just really lost
Last edited on
closed account (48T7M4Gy)
There is no harm in self-help especially when the tutorials on this site are so easily available. The chances are you will learn a lot more.

Use this as a start -> http://www.cplusplus.com/doc/tutorial/files/ There is a sample program on using a while loop and getline to read a text file which can be adapted to what you want. (You won't need getline though)

Here's a start of how to do it:

1
2
3
4
int count = 0;
while( infile >> empID[count] >> hours[count] etc && count < 21 ){
    count++;
}


BTW Please use code tags and properly indent you code. For code tags select your code and hit the <> button from the toolbox on the right.
Thanks! I still keep the arrays right?
closed account (48T7M4Gy)
Yes you keep the arrays. You also need to specify the value of SIZE ( = 20 or 21) so surplus values on the file are ignored.
Last edited on
closed account (48T7M4Gy)
Also, since your file is limited by the value -1, you might have to think carefully how the while condition might be modified. This is necessary to allow for less than 20 lines of data.

You might end up with something along the lines of what jonnin has written, ie read the first value on each line and stop reading if the vale is -1 and if it isn't keep reading the other values on the line, also provided you are still within the 20 limit.

(Hint: if you can, delete the line with -1 on it and let the machine (stream) work out the end of data via the while statement I have written.)
I have to leave what's written in the infile alone
closed account (48T7M4Gy)
That's OK, I thought that might be the case. So all you have to do is read the first value in and adjust the while condition and loop along the following lines:

1
2
3
4
5
6
int count = 0;
while( infile >> empID[count] && empID[count] != -1 && count < 21 ){
    infile >> hours[count] >> rate[count];

    count++;
}


Have a struggle with that, try things out, and make sure you print out the results to make sure you are going in the right direction.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/215317/#msg1000822
I've tried many different things, but the numbers from the inFile won't show :( I tried this,

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main() {

const int SIZE=20;

int empId[SIZE]{};
double hours[SIZE]{};
double payRate[SIZE]{};
double wages[SIZE]{};
string inFileName;




cout<<"Infile is "<<endl;
cin>>inFileName;
ifstream inFile(inFileName);



if (!inFile.is_open())
{
cout<<"Error opening file."<<endl;
}


int count = 0;


while( count!= -1 && count<21 && inFile>> empId[count])
{
inFile >> hours[count] >> payRate[count];
count++;
}

for (int i=0; i<count; i++)
{
wages[count] = hours[count]*payRate[count];
}




and this was the output.
Infile is
in7.txt
Program ended with exit code: 0


I can't get the infile in and it's bothering me. I'm getting desperate here.
Try this:

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

const int SIZE=20;

int main() 
{   int empId[SIZE];
    double hours[SIZE];
    double payRate[SIZE];
    double wages[SIZE];
    string inFileName;
    int count = 0;
    
    cout<<"Infile is "<<endl;
    cin>>inFileName;
    ifstream inFile(inFileName.c_str());

    if (!inFile.is_open())
    {   cout<<"Error opening file."<<endl;
        return 1;       //  No point in continuing
    }

    while(count<SIZE && inFile>> empId[count] && empId[count] != -1)
    {   inFile >> hours[count] >> payRate[count];            
        wages[count] = hours[count]*payRate[count];
        count++;
    }

    for (int i=0; i<count; i++)
    {   cout << empId[i] << " " << wages[i] << endl;
    }

    system ("pause");
    return 0;
}    


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

@AbstractionAnon
it works! Thanks so much!
Topic archived. No new replies allowed.