Assignment #2, Payroll Summary

Hey guys,

I am trying to have a program pull information from a data file and utilize it for the 'cin' inputs (linux redirection/batch mode). At first, I could only get the program to read the first number; since the next value was a character. I did manage to get it to read the whole line, but it doesn't go down to read the next several lines. Here is my code so far:

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
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <istream>

using namespace std;

int main() {

 //Not all of these are necessarily used yet.
 double salary;
 double wage;
 double commission;
 double temp;
 double piecework;

 //Characters and integers being pulled from the data file.                                                                                            
 char h;
 char s;
 int ID;
 int pay;
 int hours;
 //Header.                                                                                                                                             
 cout << "ID #" << setw(13) << "CATEGORY" << setw(16) << "EARNINGS" << endl;
 cout << "---------------------------------" << endl;

   while (cin) //I know this must be incorrect...
     {

       if (cin >> ID >> s >> pay)

         cout << ID << setw(13) << "Salaried" << setw(7) << "$" << setw(9) << pay << endl;

       if (cin >> ID >> h >> hours >> pay)

         cout << ID << setw(13) << "Hourly" << setw(7) << "$" << setw(9) << hours*pay << endl;
     }

 return 0;
}



I appreciate any sort of help you guys can give! Thank you.
Sounds like the problem you have is that a line could be in one of two formats:

int char int

or

int char int int

and you don't know until you read it which it is. So you'll have to add some branching logic. I assume the char indicates if the employee is hourly or salaried, with an 'h' or an 's'.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
while (cin >> ID) 
{
  char payType;
  cin >> payType;
  if (payType== 's')
  {
    // this employee is salaried
    cin >> pay;
    cout << ID << setw(13) << "Salaried" << setw(7) << "$" << setw(9) << pay << endl;
  }
  else if (payType== 'h')
  {
    // this employee is paid hourly
    cin >> hours;
    cin >> pay;
    cout << ID << setw(13) << "Hourly" << setw(7) << "$" << setw(9) << hours*pay << endl;
  }
  else
  {
    // bad value
    cout << "Unknown pay type: " << payType << " Cannot assess hourly or salaried";
  }
  cout << endl;
}

Last edited on
Topic archived. No new replies allowed.