Mail Archives: djgpp/1999/11/24/02:40:38
First of all I notice that none of the variables used are ever initialised
(specifically total)... What I would do is try and use the -wAll switch to
switch on all the warnings and see what that comes up with... A lot of those
warnings will actually be bugs in your program...
Otherwise I can't see a problem with it... If worse comes to worse, run a
debugger (Rhide would be the best bet) and step thru the program line by line
watching the variables to see what happens...
Also, why did you open a block inside the block? You could just as easily have
all the code inside the loop inside the one block.
You were asking about using a for loop only... You could take [pay] out and
replace the total with total += hr * rate tho' it's not strictly necessary :-)
Have fun,
Michael
Jason Yip wrote:
> The output of this program is always incorrect. Can anyone tells me why and
> correct it?? Thanks a lot!!
> What's more, I would like to make the prog to reject -ve input and shows a
> msg, then input again. How can I do?
> (Can I use for loop only?)
>
> #include <stdio.h>
> main ()
> {
> int i, ppl; float rate, hr, total, pay;
> printf("Please input how many employees have overtime works: ");
> scanf("%d",&ppl);
> for(i=1;i<=ppl;i++){
> {
> printf("Please input the overtime hour of the no %d employee: ", i);
> scanf("%f",&hr);
> printf("Please input the overtime hourly rate of the no %d employee: ", i);
> scanf("%f",&rate);
> pay=hr*rate;
> }
> total+=pay;
> }
> printf("The total overtime pay is %.2f",total);
> return 0;
> }
- Raw text -