Is this right?

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.

The program should relate the data in each array through the subscripts. For example, the number in element 0 of the hours should be the number of hours worked by the employee whose identification number is stored in element 0 of the empId array. That same employee's pay rate should be stored in element 0 of the payRate array."


Here's what I have so far:




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


int main() {

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



string inFileName;


ifstream inFile;
inFile.open(inFileName);


int count = 0;
while( inFile >> empId[count] >> hours[count] >> payRate[count] && count < 21 ){
count++;
}

wages[count] = payRate[count]*hours[count];

return 0;
}
Last edited on
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
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    // If an attempt is made to enter 21, process only the first 20.
    const int SIZE = 20 ;

    int empId[SIZE] {} ; // initialise to all zeroes
    double hours[SIZE] {} ; // initialise to all zeroes
    double payRate[SIZE] {} ; // initialise to all zeroes
    double wages[SIZE] {} ; // initialise to all zeroes

    const std::string inFileName = "wages.txt" ; // place the name of the actual file here
    std::ifstream inFile( inFileName) ; // open the file for input

    if( !inFile.is_open() )
    {
        std::cerr << "could not open input file '" << inFileName << "'\n" ;
        return 1 ; // return non-zero to indicate failure
    }

    // file was opened
    int count = 0 ;
    // check that count < SIZE first
    while( count < SIZE && inFile >> empId[count] >> hours[count] >> payRate[count] ) ++count ;

    for( int i = 0 ; i < count ; ++i )
    {
        // compute wages
        // etc.
    }
}
@imastruggler

Please don't double post, it ultimately a time waster for those who reply. You had a bunch of good replies to the other post :

http://www.cplusplus.com/forum/beginner/215307/

If you still don't understand, then continue with the original topic, and say why. Don't start a new topic :+)

Edit: Always use code tags: http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Topic archived. No new replies allowed.