Mail Archives: djgpp/1999/11/23/13:37:54
"Jason Yip" <manman AT netteens DOT net> writes:
> The output of this program is always incorrect. Can anyone tells me why and
> correct it?? Thanks a lot!!
Your `total' variable is uninitialized. So it starts at some random
value, which is then added to, and of course the answer is wrong. You
should add `total = 0.0' somewhere before the loop.
> 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?)
A `while' would be simpler...
printf("Input rate");
scanf("%f", &rate);
while (rate < 0.0)
{
printf("Try again");
scanf("%f", &rate);
}
> #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;
> }
>
>
--
Nate Eldredge
neldredge AT hmc DOT edu
- Raw text -