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 October 18th, 2005, 08:03 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
Very Beginner Loop question

The program reads employee number, num of shifts, and base rate from a file (each line at a time using while). then, using the for statement, depending on the number of shifts, it reads the number of hours per shift. So, if a person worked 2 shifts, the for statement would run twice, adding onto the value of "total_hours".

Code:
#include <stdio.h>

int
main()
{
int emp_num, num_shifts, i;
double base_rate, num_hours, total_hours;

	while (scanf("%d %d %lf %lf", &emp_num, &num_shifts, &base_rate)!=EOF)
	{

	for(i=0; i<num_shifts; i++) {
		scanf("%lf", &num_hours);
		
		total_hours = total_hours + num_hours;
			
	}

	printf("%d %d %f %f %f", emp_num, num_shifts, base_rate, total_hours);
	
	}
return(0);
}


My input file is the following:
Quote:
000 1 8.00 10
001 2 7.50 7 8
002 4 7.00 5 5 5 5

and i used the redirection option in my compiler, by simply inputting "<input.txt".

thank you for any help you can provide.

Reply With Quote
  #2  
Old October 18th, 2005, 08:43 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
What does it mean to equate scanf statements to a value?

PHP Code:
if ( scanf("%lf", &num_hours) == 1


and
PHP Code:
while ( scanf("%d %d %lf", &emp_num, &num_shifts, &base_rate) == 


Please explain how these above functions work.

Reply With Quote
  #3  
Old October 18th, 2005, 11:13 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
umm.. dude this place is for C++ not PHP but I think they got a section for that programming style.

Reply With Quote
  #4  
Old October 19th, 2005, 02:42 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 992 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 734494 Folding Title: Super Ultimate Folder - Level 2Folding Points: 734494 Folding Title: Super Ultimate Folder - Level 2Folding Points: 734494 Folding Title: Super Ultimate Folder - Level 2Folding Points: 734494 Folding Title: Super Ultimate Folder - Level 2Folding Points: 734494 Folding Title: Super Ultimate Folder - Level 2Folding Points: 734494 Folding Title: Super Ultimate Folder - Level 2Folding Points: 734494 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 13 h 37 m 16 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Quote:
Originally Posted by BloodlustShaman
umm.. dude this place is for C++ not PHP but I think they got a section for that programming style.


Dude, that IS C he's writing, he just used the wrong highlighting tags.

Quote:
Originally Posted by dekoi
What does it mean to equate scanf statements to a value?

PHP Code:
if ( scanf("%lf", &num_hours) == 1

and
PHP Code:
while ( scanf("%d %d %lf", &emp_num, &num_shifts, &base_rate) == 

Please explain how these above functions work.


the scanf() function returns a value, depending on the 'succes' it's had doing it's job. When things go (relatively) well, it returns the number of items read. When it can't read ALL the parameters, this value is then smaller than the number of argumenst expected.

If no arguments could be read at all, it rerturns EOF (end of file).
See here for more details.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 275

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Very Beginner Loop question


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway