C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 11th, 2007, 08:30 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
File Input/Output Problem

Hello everyone. I'm working on a program which reads information from a file, multiplies a couple numbers, then outputs column headings (read from the original file) and the numbers which were multiplied. I am having a problem reading in the information, then outputting just the info that I need. I haven't even attempted the manipulation of the numbers. I have some info commented out for my own troubleshooting.

Code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void main()
{
	int HoursWorked=0,TotalPay=0,IDNumber=0,PayRate=0;
	char FirstName[12]={0}, LastName[12]={0};
	ifstream ifp;
	ofstream ofp;
	ifp.open("c:\\Documents and Settings\\Jeff Young\\Desktop\\Lab14.in");
	ofp.open("c:\\Pay.out");
//	ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate;
	while (ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate)
	{
		if (HoursWorked<=40&&HoursWorked>0)
		{
			TotalPay=(HoursWorked*PayRate);
		}
		else (HoursWorked>40);
		{
			TotalPay=(40*PayRate)+(HoursWorked-40)*PayRate*1.5;
		}
		cout<<setw(10)<<FirstName<<"   "<<setw(10)<<LastName<<"   "<<setw(10)<<"IDNumber"<<"    "<<setw(10)<<"HoursWorked"<<"  "<<setw(10)<<"PayRate"<<"   "<<endl;
//		cout<<LastName<<"    "<<setw(10)<<"IDNumber"<<"   "<<setw(10)<<"TotalPay"<<endl<<endl;
	}
//	cout<<setw(10)<<FirstName<<"   "<<setw(10)<<LastName<<"   "<<setw(10)<<"IDNumber"<<"    "<<setw(10)<<"HoursWorked"<<"  "<<setw(10)<<"PayRate"<<"   "<<endl;
//	cout<<LastName<<"    "<<setw(10)<<"IDNumber"<<"   "<<setw(10)<<"TotalPay"<<endl<<endl;
}


Thanks,
J

Reply With Quote
  #2  
Old June 11th, 2007, 11:21 AM
patrick4623 patrick4623 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 86 patrick4623 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 20 h 41 m 50 sec
Reputation Power: 12
i can't see any problem from your program,jus the ofstream u can cancel it bcos it no use in your pogram........
or u try to put out your infile mayb is infile problem......

Reply With Quote
  #3  
Old June 11th, 2007, 11:22 AM
ahammad ahammad is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 43 ahammad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 41 m 19 sec
Reputation Power: 11
The condition for your while loop seems off. What you want to do is to keep reading in data until the end-of-file is reached, right?

Well, for your convenience, there is already a function that does that. So replace the while loop condition with:

Code:
while (!ifp.eof())	{
		ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate;
...
}


Basically, this loop says to keep reading in the data until eof() becomes true.

eof() is actually End Of File. If it is true, then the end of the file has been reached.

I don't understand the second part of the problem. Can you clarify it?

Reply With Quote
  #4  
Old June 11th, 2007, 11:25 AM
ahammad ahammad is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 43 ahammad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 41 m 19 sec
Reputation Power: 11
I forgot to add...put the input file in the same directory as your cpp file and instead of doing this:

("c:\\Documents and Settings\\Jeff Young\\Desktop\\Lab14.in");


do this:

("Lab14.in")

That way you can eliminate more chances of problems.

Reply With Quote
  #5  
Old June 11th, 2007, 11:35 AM
patrick4623 patrick4623 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 86 patrick4623 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 20 h 41 m 50 sec
Reputation Power: 12
but i more prefer hypnotik reading type bcos i have try before if u using the eof function to read it then output all the thing fron the file u will get a empty object so if u doing sorting the empty object will have in front.......it will have problem if u sort it but if not then is ok.............

Reply With Quote
  #6  
Old June 11th, 2007, 11:42 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
I have the ofstream in the program because it eventually needs to be printed to an output file. I just hadn't added that because it wasn't printing to the screen correctly.

I guess the second part of the problem is, the display window simply reads the "press any key to continue", it's not displaying any data from the file.

J

Reply With Quote
  #7  
Old June 11th, 2007, 12:00 PM
patrick4623 patrick4623 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 86 patrick4623 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 20 h 41 m 50 sec
Reputation Power: 12
do you have put a system pause????
try to cancel it............

Reply With Quote
  #8  
Old June 11th, 2007, 12:02 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Quote:
Originally Posted by patrick4623
do you have put a system pause????
try to cancel it............




I don't know what a system pause is.

J

Reply With Quote
  #9  
Old June 11th, 2007, 12:14 PM
patrick4623 patrick4623 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 86 patrick4623 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 20 h 41 m 50 sec
Reputation Power: 12
i realise the problem....
in your cout you din put the variable for the program
you should do like tis............
cout<<setw(10)<<FirstName<<" "<<setw(10)<<LastName<<" "<<setw(10)<<"IDNumber"<<IDNumber<<" "<<setw(10)<<"HoursWorked"<<HoursWorked<<" "<<setw(10)<<"PayRate"<<PayRate<<" "<<endl;

Last edited by patrick4623 : June 11th, 2007 at 12:18 PM.

Reply With Quote
  #10  
Old June 11th, 2007, 12:56 PM
patrick4623 patrick4623 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 86 patrick4623 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 20 h 41 m 50 sec
Reputation Power: 12
it is the problem????
i m not sure for it........
hope tat help...........

Reply With Quote
  #11  
Old June 11th, 2007, 01:00 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Well I made that change, although I'm still not getting any of the information from the file to display on the screen.

J

Reply With Quote
  #12  
Old June 11th, 2007, 02:06 PM
nosale nosale is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 110 nosale User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 23 m 10 sec
Reputation Power: 11
Hey guys,

Jeez... reading through that was like watching two monkeys try to hump a football. Suggestion: if you're not sure what the problem is, don't try to fix it!

There is nothing wrong with your code... I compiled and tested it. Try adding this line right after your ifp.open(...):
c++ Code:
Original - c++ Code
  1. if(!ifp.good()) {
  2.   cout << "Error opening file for input!\n";
  3.   return 0;
  4. }

and if you are getting into that conditional, then your problem is your file name string. If thats not causing the problem, then the only other reason I could see your program not working is that your input file is not formatted the way you think it is. Post the input file up here and I can help you with that.

EZ-E

Reply With Quote
  #13  
Old June 11th, 2007, 02:48 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Here is the input file:

FirstName LastName IDNumber HoursWorked PayRate
Jeff Young 100 40 10.00
Sarah Young 101 38 8.00
Emily Yarbs 102 12 7.00
Jessica Yarbs 103 27 7.00
Tommy Young 104 42 8.00
Tom Young 105 37 9.00
Joyce Young 106 23 8.00
Clem Muddy 107 40 10.00
Audrey Muddy 108 17 7.00
Paul Muddy 109 10 8.00

Reply With Quote
  #14  
Old June 11th, 2007, 02:55 PM
ahammad ahammad is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 43 ahammad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 41 m 19 sec
Reputation Power: 11
Code:
FirstName LastName IDNumber HoursWorked PayRate


There is your problem. IDNumber, HoursWorked, and PayRate are all strings, but when the program reads in the very first line, it takes in those strings and tries to assigne them to some integer variables.

Erase that first line and try it out.

Reply With Quote
  #15  
Old June 11th, 2007, 02:58 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
I want to repost the code...so we're all looking at the same code.


Code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void main()
{
	int HoursWorked=0,TotalPay=0,IDNumber=0,PayRate=0;
	char FirstName[12]={0}, LastName[12]={0};
	ifstream ifp;
	ofstream ofp;
	ifp.open("c:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\Lab 14.in"); 
	ofp.open("c:\\Pay.out");
	ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate;
	while (ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate)
	{
		if (HoursWorked<=40&&HoursWorked>0)
		{
			TotalPay=(HoursWorked*PayRate);
		}
		else(HoursWorked>40);
		{
			TotalPay=(40*PayRate)+(HoursWorked-40)*PayRate*1.5;
		}
		cout<<LastName<<"    "<<setw(10)<<"IDNumber"<<"   "<<setw(10)<<"TotalPay"<<endl<<endl;
		cout<<setw(10)<<FirstName<<" "<<setw(10)<<LastName<<" "<<setw(10)<<"IDNumber"<<IDNumber<<" "<<setw(10)<<"HoursWorked"<<HoursWorked<<" "<<setw(10)<<"PayRate"<<PayRate<<" "<<endl;
	}
//	cout<<setw(10)<<FirstName<<"   "<<setw(10)<<LastName<<"   "<<setw(10)<<"IDNumber"<<"    "<<setw(10)<<"HoursWorked"<<"  "<<setw(10)<<"PayRate"<<"   "<<endl;
//	cout<<LastName<<"    "<<setw(10)<<"IDNumber"<<"   "<<setw(10)<<"TotalPay"<<endl<<endl;
//	cout<<setw(10)<<FirstName<<" "<<setw(10)<<LastName<<" "<<setw(10)<<"IDNumber"<<IDNumber<<" "<<setw(10)<<"HoursWorked"<<HoursWorked<<" "<<setw(10)<<"PayRate"<<PayRate<<" "<<endl;

}

Reply With Quote
  #16  
Old June 11th, 2007, 03:13 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Sorry for multiple posts back-to-back. I'm an idiot. My file path was wrong...so I'm getting closer.

J

Reply With Quote
  #17  
Old June 11th, 2007, 03:40 PM
ahammad ahammad is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 43 ahammad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 41 m 19 sec
Reputation Power: 11
Did you try my suggestion?

Reply With Quote
  #18  
Old June 11th, 2007, 03:44 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Quote:
Originally Posted by ahammad
Did you try my suggestion?


Yes. And along with making the file path correct it works pretty well. I receive some negative numbers, but I'm still working on why that could be.

The assignment requires the headings to be in the input file, but I'm leaving them out for now...until I hear from the instructor.


Thanks everyone for all the help...

J

Reply With Quote
  #19  
Old June 11th, 2007, 04:01 PM
ahammad ahammad is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 43 ahammad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 41 m 19 sec
Reputation Power: 11
Good, so now you narrowed it down.

I tried to compile the code you have there, and there are a few issues with doubles and ints you may want to check out.

As for keeping the titles in the file, you can have a counter, and have something like (if (counter ==0)) type statment, then ignore inputs.

Or you can have two places to read the data, with the first one just to read one line as a string, then the second one to read the rest of the file.

Reply With Quote
  #20  
Old June 11th, 2007, 04:04 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
I tried to take the input in, in two spots and it doesn't seem to work. In the code I have ifp>>....... then I have the while (ifp>>>...)

I don't know. I'm messing with int's and floats now, and also trying to figure out why the negative numbers are showing up. The math is NOT correct either for most of the numbers

J

Reply With Quote
  #21  
Old June 11th, 2007, 04:57 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Finally Finished

Well, it's amazing what a misplaced ; does.

Everything is working great.

Thanks again for those that helped I really appreciate it.

On to the next assignment.

J

Reply With Quote
  #22  
Old June 12th, 2007, 12:24 PM
patrick4623 patrick4623 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Posts: 86 patrick4623 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 20 h 41 m 50 sec
Reputation Power: 12
Quote:
Originally Posted by nosale
Hey guys,

Jeez... reading through that was like watching two monkeys try to hump a football. Suggestion: if you're not sure what the problem is, don't try to fix it!

There is nothing wrong with your code... I compiled and tested it. Try adding this line right after your ifp.open(...):
c++ Code:
Original - c++ Code
  1. if(!ifp.good()) {
  2.   cout << "Error opening file for input!\n";
  3.   return 0;
  4. }

and if you are getting into that conditional, then your problem is your file name string. If thats not causing the problem, then the only other reason I could see your program not working is that your input file is not formatted the way you think it is. Post the input file up here and I can help you with that.

EZ-E






is oso useless to add those line...................

Reply With Quote
  #23  
Old June 12th, 2007, 01:09 PM
nosale nosale is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 110 nosale User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 23 m 10 sec
Reputation Power: 11
What?

Are you trying to say it is also useless to add those lines of code?

Right... not here to get into a fight, but maybe keep that kind of stuff to yourself... especially if you can't even articulate it properly. Hope, for all our sakes, you don't end up coding the next aircraft-pilot interface.

EZ-E

Quote:
Originally Posted by patrick4623
is oso useless to add those line...................

Reply With Quote
  #24  
Old June 14th, 2007, 09:52 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Array of Stucts now!!!!!!

Ok, just when I thought I was done with this problem...our next assignment is to create a struct to hold this data, and to declare 25 of these structs.

I'm confused as to where I should declare my variables, such as HoursWorked, LastName, etc. I believe I need to declare them in the struct.

If this is so, how do I read the info from the file in??? Dot notation??

Thanks,
J

Reply With Quote
  #25  
Old June 14th, 2007, 10:34 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
Problem found.

Reply With Quote
  #26  
Old June 14th, 2007, 11:03 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
I must now pass the structure to a function to output the avg of the TotalPay variable. I receive the average TotalPay of the last TotalPay variable in the list.

Any suggestions?????

Code is below

Code:

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
struct Person
{
	double IDNumber,HoursWorked,PayRate,TotalPay[26];
	char FirstName[10],LastName[10];
};

double Average (Person Person_Info[])
{
	double sum=0.0,avg=0.0;
	int j=0;
	for (j=0;j<25;++j)
	{
		sum=sum+Person_Info[j].TotalPay[j];
	}
	avg=sum/25;
	cout<<avg;
	return (avg);
}
int main ()
{
	int i=0;
	Person Person_Info[25];
	ifstream ifp;
	ofstream ofp;
	ifp.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\Lab 3 External Files and Structs\\Lab2.in");
	ofp.open("C:\\Pay.out");
//	cout<<"LastName"<<"     "<<"IDNumber"<<"     "<<"TotalPay"<<endl;
	ofp<<"LastName"<<"     "<<"IDNumber"<<"     "<<"TotalPay"<<endl;
	while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
	{
		if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)					
		{
			Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);				
		}
		else if (Person_Info[i].HoursWorked>40)						
		{
			Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
		}
	//	cout<<Person_Info[i].FirstName<<"     "<<Person_Info[i].LastName<<Person_Info[i].IDNumber<<"    "<<Person_Info[i].HoursWorked<<"     "<<Person_Info[i].PayRate<<"     "<<Person_Info[i].TotalPay[i]<<endl;
	}
	Average(Person_Info);

	
}


Thanks,
J

Reply With Quote
  #27  
Old June 14th, 2007, 03:39 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 11
PROGRAM Finished

Hey guys, found the problem.

J

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File Input/Output Problem


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap