Homework Help

Hi,
I am a CS student and currently have a lab that is due tomorrow, i've been confused and researching all week but still cant figure it out. I need to g++ compile on UNIX my c++ source with a input file redirection which I get but the issue is writing the c++ program and then accepting the input from the file.
The part of the assignment I am struggling with is that. Here is the information I have, and also what I have done. Im just hoping someone can point me in the right direction so I can figure this out.
--------------------------------------------------------------------

Program must read 10 employees information in format of
ID# FIRST LAST DOB . DATE OF HIRE PAY AND HOURS WORKED

input file looks like:
57383 JOHN DOE 10 20 1981 6 5 2005 8.75 36

compute gross pay / net pay after tax reduction
output info to console

heres my EMPLOYEEINFO.H

#ifndef EmployeeInfo_h
#define EmployeeInfo_h
#include "Date.h"
#include <string>

struct EmployeeInfo {
int IDNUM, PAYRATE; // id number, pay rate
std::string FIRST, LAST; // first and last name
Date DOB;
Date DOH;

};

----------------------------------------------

and my main.cpp


#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "EmployeeInfo.h"
#define MAX 10
using namespace std;

int main()
{
char file[20];
int num;
ifstream fin;
do
{
fin.clear();
cout << "enter name of file to connect to" << endl;
cin.getline(file, 20);
fin.open(file);
fin >>num;
} while(!fin);


Thanks!
Chris
redirection works exactly as if the user had typed from the keyboard.

int main()
{
string s;
cin >> s;
cout << s << endl;
return 0;
}

.. g++ filename -stuff
...
./a.out << file
should spit the file's contents to the console.


Okay so I write the program to compute all the gross pay and simple math and set string as cin and it automatically grabs the input from file as if the user typed it?
yes, so long as you do the redirection when you run the program. And it helps to get the syntax correct, I think it is actually a.out < file (not <<)
but i want it to read
Name:
ID:
DOB:
DOH:
Hours:
Pay Rate:
Gross Pay:
Tax:
Net Pay:

how does it pull the info as
57383 JOHN DOE 10 20 1981 6 5 2005 8.75 36

in the input file and place it in the correct structure
You have to cin each variable you want to populate as if reading from a keyboard..

cin>> id_variable;
cin>> dob_variable;
...etc

the loop to read a multi-record file
It might be something like

char ch = 0;

while(ch != EOF)
{
cin... stuff

ch = cin.peek(); // this trusts that the file is formatted correctly, with data for each record and then end of file.
}
Last edited on
Topic archived. No new replies allowed.