| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
and i used the redirection option in my compiler, by simply inputting "<input.txt". thank you for any help you can provide. |
|
#2
|
|||
|
|||
|
What does it mean to equate scanf statements to a value?
PHP Code:
and PHP Code:
Please explain how these above functions work. |
|
#3
|
|||
|
|||
|
umm.. dude this place is for C++ not PHP but I think they got a section for that programming style.
|
|
#4
|
||||
|
||||
|
Quote:
Dude, that IS C he's writing, he just used the wrong highlighting tags. Quote:
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
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Very Beginner Loop question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|