GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Mar-2005, 11:31
Requoter Requoter is offline
New Member
 
Join Date: Mar 2005
Posts: 9
Requoter is on a distinguished road
Question

homework problem


Hi everybody! I've been working on this program, that's due tomorrow and I have stumbled upon a problem that I can't figure out.
The program I'm doing suppose to input into the array names, id-numbers and years of service of the employees of non-existent company, the input is about 5 lines total each line looks kinda like this: Jason C. Greenway 642 8, and then I'm suppose to sort it all out by last name, id-number etc. I haven't done the sorting part yet, coz I'm having problems with input file that just wouldn't read into an array. Can anyone tell me what am I doing wrong?
Here's the program:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main() 
{ 
	ifstream inData;
	ofstream outData;

	inData.open("lab3.dat");
	outData.open ("lab3.out");

struct Employee
	{
		string firstName;
		string middleInitial;
		string lastName;
		int empNum;
		int yearsofService;
	}   employeeList[10];

	int count = 0;

while (inData)
	{	
		inData >> employeeList[10].firstName >> employeeList[10].middleInitial 
		   >> employeeList[10].lastName >> employeeList[10].empNum 
		   >> employeeList[10].yearsofService;
		count ++;
	}


// Testing the state of the stream
if (!inData)
{
cout << "Can't open the input file." << endl << endl;
return 1;
}

// Output
cout << setw(40) << "Employee's List" << endl << endl;

for (int i = 0; i < count; i++)

{
	cout << setw(9) << employeeList[i].firstName << " " << employeeList[i].middleInitial 
		 << " " << employeeList[i].lastName << " " << employeeList[i].empNum 
		 << " " << employeeList[i].yearsofService << endl;
}

	cout << endl;

	outData << setw(60) << "Employee's List" << endl << endl;
	outData << setw(9) << "Name:" << setw(16) << "ID:" << setw(7) << "Years:" 
		    << endl << endl;

for (i = 0; i < count; i++)

{
	outData << setw(9) << employeeList[i].firstName << " " << employeeList[i].middleInitial 
		    << " " << employeeList[i].lastName << " " << employeeList[i].empNum 
			<< " " << employeeList[i].yearsofService << endl;
}
	inData.close();
	outData.close();

return 0;

}

Thanks in advance for your help.
  #2  
Old 23-Mar-2005, 16:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
You have defines your employee array to hold 10 employees, numbered 0 to 9. When you read the file:
CPP / C++ / C Code:
while (inData)
{	
    inData >> employeeList[10].firstName >> employeeList[10].middleInitial 
           >> employeeList[10].lastName >> employeeList[10].empNum 
           >> employeeList[10].yearsofService;
    count ++;
}
you are reading into the 11th entry in your array for each read. Try changing 10 to count to load the data into the correct array position.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #3  
Old 23-Mar-2005, 22:37
Requoter Requoter is offline
New Member
 
Join Date: Mar 2005
Posts: 9
Requoter is on a distinguished road
Quote:
Originally Posted by WaltP
Try changing 10 to count to load the data into the correct array position.

I've tried that before, but it didn't work.
  #4  
Old 25-Mar-2005, 09:36
Requoter Requoter is offline
New Member
 
Join Date: Mar 2005
Posts: 9
Requoter is on a distinguished road
Quote:
Originally Posted by Requoter
I've tried that before, but it didn't work.

I finally got it done, right before class, and it worked. But the thing I added didn't really make much sense. Turned out that in the for loop I had to put i < count - 1 which meant that it went through the loop 7 times although I only had 5 lines in my data file. Weird...
  #5  
Old 26-Mar-2005, 08:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,617
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Requoter
Weird...

Well, it's not weird at all, but it is kind of important, since your program shows something that is quite common, and is easy to understand (if you are interested) and easy to fix.

I have abstracted your file reading process to the following:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>

using std::ifstream;
using std::cout;
using std::endl;
using std::string;

int main()
{
  ifstream inData("test.txt");
  string line;

  while (inData) {  
    inData >> line;
    cout << line << endl;
  }
  return 0;
}


Here is the test.txt file:

CPP / C++ / C Code:
Line1
Line2
Line3

Here is the result of running the program:

Quote:
Line1
Line2
Line3
Line3

The program printed the last line of the file twice. The reason is that the loop condition: while(inData) remains true until after the program has tried to read beyond the end of file. That means that the program runs through the loop after line 3 has been read. Trying to read more yields nothing from the file, but the variable line still has its old value: the last line that was read.

If you want to use this procedure for file reading, you could do something like this:

CPP / C++ / C Code:
  while (inData) {  
    inData >> line;
    if (inData) {
      cout << line << endl;
    }
  }

Now, it only prints if inData is not in its "fail" state (hasn't tried to read past the end of file). The output is this:
Quote:
Line1
Line2
Line3

There may be more elegant and robust ways of reading different things for files, but the principles are the same: test what you got before using it.

Regards,

Dave
  #6  
Old 26-Mar-2005, 09:29
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by davekw7x
If you want to use this procedure for file reading, you could do something like this:

CPP / C++ / C Code:
  while (inData) {  
    inData >> line;
    if (inData) {
      cout << line << endl;
    }
  }
...

There may be more elegant and robust ways of reading different things for files, but the principles are the same: test what you got before using it.
Not saying this is better, it's just different:
CPP / C++ / C Code:
inData >> line;
do
{
    cout << line << endl;
    inData >> line;
} while (inData);
This does assume there is at least one line in the file. If not, this also has problems so yo can put the if around the do/while.

One reason I like this is it removes the if from the processing loop so only one comparison is needed. The thing I don't like is you have to set up two reads.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #7  
Old 26-Mar-2005, 09:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,617
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by WaltP
Not saying this is better, it's just different:

One reason I like this is it removes the if from the processing loop so only one comparison is needed. The thing I don't like is you have to set up two reads.

Actually, I rarely use either type. More often it's something like this:

CPP / C++ / C Code:
  while(getline(inData, line)) {
    // typically do something more useful with the input line here
    cout << line << endl;
  }

But I did want to illustrate the point about reading after the end of the file. Your example is another way. Neither is particularly elegant (as far as my personally-defined sense of elegance goes), but they do show ways of getting it done.

Regards,

Dave
  #8  
Old 28-Mar-2005, 10:35
Requoter Requoter is offline
New Member
 
Join Date: Mar 2005
Posts: 9
Requoter is on a distinguished road
Now, I'm stuck on a different problem.

In my original program I need to take the printing part and make it a void function, which I have no idea how to do, coz I never really had those before and my book doesn't explain much ;( The main problem is I'm getting confused on declaring parameters for the void function. Plus the structure makes it even more compilcated.

Here's what I've done so far:

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

void printIt (ofstream& outData, int, int);

int main() 
{ 
	ifstream inData;
	ofstream outData;

	inData.open("lab3.dat");
	outData.open ("lab3.out");

struct Employee
	{
		string firstName;
		string middleInitial;
		string lastName;
		int empNum;
		int yearsofService;
	}   employeeList[10];

int count = 0;
int i;

// reading data
while (inData)
	{
		inData >> employeeList[count].firstName >> employeeList[count].middleInitial 
		   >> employeeList[count].lastName >> employeeList[count].empNum 
		   >> employeeList[count].yearsofService;
	count++;
	}

printIt (outData, count, i);

inData.close();
outData.close();

return 0;
}

void printIt (ofstream& outData, int count, int i)

{

struct Employee
	{
		string firstName;
		string middleInitial;
		string lastName;
		int empNum;
		int yearsofService;
	}   employeeList[10];

count = count--;

	cout << setw(25) << "Employee List:" << endl << endl;
	cout << setw(9) << "Name:" << setw(18) << "ID:" << setw(8) << "Years:" 
		    << endl << endl;

for (i = 0; i < count ; i++)

{
	cout << setw(9) << employeeList[i].firstName << " " << employeeList[i].middleInitial 
		 << " " << employeeList[i].lastName << "   " << employeeList[i].empNum 
		 << "    " << employeeList[i].yearsofService << endl;
}

	cout << endl;

	outData << setw(25) << "Employee List:" << endl << endl;
	outData << setw(9) << "Name:" << setw(18) << "ID:" << setw(8) << "Years:" 
		    << endl << endl;

for (i = 0; i < count ; i++)

{
	outData << setw(9) << employeeList[i].firstName << " " << employeeList[i].middleInitial 
		    << " " << employeeList[i].lastName << "   " << employeeList[i].empNum 
			<< "    " << employeeList[i].yearsofService << endl;
}

}

it actually compiles it and all I get is this warning warning C4700: local variable 'i' used without having been initialized, but when I execute it here's what my my output looks like:

Employee List:

Name: ID: Years:

-858993460 -858993460
-858993460 -858993460
-858993460 -858993460
-858993460 -858993460
-858993460 -858993460


Where did I screw up?
  #9  
Old 28-Mar-2005, 14:16
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
The problem I believe is you redefined employeeList[] in the function so you are printing out bogus values. You need to pass the structure as a parameter to printIt().
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #10  
Old 28-Mar-2005, 22:39
Requoter Requoter is offline
New Member
 
Join Date: Mar 2005
Posts: 9
Requoter is on a distinguished road
Quote:
Originally Posted by WaltP
You need to pass the structure as a parameter to printIt().

That did it. Thanks!
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help! Insert into database problem robsmith MySQL / PHP Forum 1 24-Apr-2005 03:44
Need advice on a disturbing problem JUNK KED Open Discussion Forum 6 31-Mar-2005 13:51
Need Help Starting the following problem helpme C Programming Language 1 24-Nov-2004 14:44
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 09:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.