What stupid basic thing am I missing? we are to read in from a file that contains 10 lines.....name, age, etc....I can read in the file ok and print out the first line....I am having problems printing subsequent lines....HELP!! lol

//Acme Personel Report
//by PAtrick Nealey

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <fstream>

using namespace std;

const int MAX_NAME_LENGTH = 21;
const int MAX_DEPT_LENGTH = 16;

void getdata(ifstream& infile,ofstream& outputfile, char name[MAX_NAME_LENGTH], int& years, char department[MAX_DEPT_LENGTH], double& ytdpay);



int main()
{
	char name[MAX_NAME_LENGTH];
	int years;
	char department[MAX_DEPT_LENGTH];
	double ytdpay;

	ifstream infile ("PJ811_personnel.txt");
	if(!infile)
	{
		cerr <<"Could not open input file";
		return -1;
	}

	

	ofstream outputfile ("PJ811_OUTPUT_Nealey.txt");
	if (!outputfile)
	{
		cerr <<"Unable to open output file.\n";
	}
	outputfile <<setw(40)<<"Acme Personnel Report"<<endl<<endl;
	outputfile <<left<<setw(20)<<"Employee"<<setw(8)<<"Years"<<left<<setw(15)<<"Department"<<right<<setw(10)<<"Year to"<<endl;
	outputfile <<left<<setw(20)<<"Name"<<setw(8)<<"Emp."<<left<<setw(15)<<"Name"<<right<<setw(10)<<"Date Pay"<<endl<<endl;
	
	getdata(infile,outputfile, name, years, department, ytdpay);
	


	return 0;
}
void getdata(ifstream& infile,ofstream& outputfile, char name[MAX_NAME_LENGTH], int& years, char department[MAX_DEPT_LENGTH], double& ytdpay)
{
	while (!infile.eof())
	{
	infile.get(name, MAX_NAME_LENGTH)>>ws;
	infile >> years>>ws;
	infile.get(department, MAX_DEPT_LENGTH)>>ws;
	infile >> ytdpay;

	
	}
	outputfile <<left<<setw(20)<<name<<right<<setw(5)<<years<<"   "<<left<<setw(15)<< department<<right<<setw(10)<<ytdpay<<endl;
}

I know using eof is not right....this is just where i decided to seek expert help

when I place that line into the loop, i get stuck in an endless loop.....

Samuel Spade 10 Automotive 22020.45
Annie Smith-Johnston 4 Appliances 25123.96
Dietrick Jones 15 Automotive 29450.88
John J. Jones, Jr. 8 Automotive 19555.55
Melissa Armstrong 12 Accounting 25945.66
Thomas Eaglebeak 6 Appliances 22222.22
William K. Woodward 18 Automotive 29877.44
Helen Trout 6 Appliances 20992.82
James Dean, II 11 Accounting 24456.66
Gerry Smith 7 Automotive 23456.77

basically the point of this exercise is to read in the txt file and print it out the same way.....only using c strings instead of c++ strings.....don't know if that helps

Like I said, reading too much data with the get()

while (!infile.eof())
  {
    infile.get(name, MAX_NAME_LENGTH)>>ws;
    cout << "Debug:==" << name << "==\n";
    infile >> years>>ws;
    infile.get(department, MAX_DEPT_LENGTH)>>ws;
    infile >> ytdpay;
    
    if ( infile.fail() ) {
      cout << "It's dead Jim!\n";
      break;
    }
  }

Now I get

$ g++ foo.cpp
$ ./a.out 
Debug:==Samuel Spade 10 Auto==
It's dead Jim!

Yes I got a bunch of
Debug:====
Debug:====
until I stopped it scrolling madly off the screen.

Where your file is

$ head PJ811_personnel.txt
Samuel Spade 10 Automotive 22020.45
Annie Smith-Johnston 4 Appliances 25123.96

Having munched 20 characters, the stream has the unlikely prospect of converting "motive" into an integer. This of course fails, and this sets the fail() to true (but leaves the eof() false).

So it enters an infinite loop of failing to do any more conversions because the stream is now in an error state.

You go and study the fstream methods and string methods and figure out another way of solving the problem.

- You had an idea
- You tried it - it didn't work (everybody gets here at some point)
- then you try to understand WHY your idea didn't work (now you're here)

So go back to the beginning and think up a fresh idea and try that.
Only this time, you know a bit more about the problem so hopefully you'll make a better attempt.

What I do not get....am I reading it in wrong?? I can print out one line correctly.....It has to be in my loop to print out the data....I have tried many ways of doing it, but get either a loop or nothing.....

This loops, reading a whole line, one line at a time.

char line[200];
while ( infile.get(line,sizeof(line)) ) {
}

The problem is, your names have a variable number of spaces. So you can't just use one of the standard file input methods to do the parsing for you.

The best you can do is read the whole line in, then work your way through it to identify each of the fields.

I understand what you are saying, but I am not sure how to parse the line once i read it in...also, does this conform to using c strings rather than c++ strings?

for ( i = 0 ; line[i] != '\0' ; i++ ) {
  if ( line[i] .... 
}

Parsing is simply walking along the line one character at a time, then making decisions based on what you've seen already and what is immediately in front of you.

Some parsers "look ahead", but I don't see that being necessary in this case. Once you've got past the name, it seems pretty easy going.

This article has been dead for over six months. Start a new discussion instead.