From: Nate Eldredge Newsgroups: comp.os.msdos.djgpp Subject: Re: Please help me to find out the problem. Date: 23 Nov 1999 09:39:36 -0800 Organization: InterWorld Communications Lines: 49 Message-ID: <83zow5m7jb.fsf@mercury.st.hmc.edu> References: <81eabb$n6o$1 AT imsp026 DOT netvigator DOT com> NNTP-Posting-Host: mercury.st.hmc.edu X-Trace: nntp1.interworld.net 943378850 55318 134.173.45.219 (23 Nov 1999 17:40:50 GMT) X-Complaints-To: usenet AT nntp1 DOT interworld DOT net NNTP-Posting-Date: 23 Nov 1999 17:40:50 GMT X-Newsreader: Gnus v5.7/Emacs 20.4 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com "Jason Yip" 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 > 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