[英]C : How to stop the loop when getting the end of input from keyboard or file while using scanf one char at a time
[英]How to stop a C while loop at once when we keyboard input without going on to the next step?
运行该程序后,当我输入-999时必须停止。 但是即使执行-999,它也会停止。 我在这里做错了什么?
这是代码的问题。
输入员工编号和员工的工作时间,并显示以下内容:员工编号,超时工资以及超时工资超过Rs的员工百分比。 4000 / - 。 用户应输入–999作为员工编号以结束该程序,并且正常的超时费用为每小时Rs.150和Rs。 每小时200个小时,超过40个小时的小时。
#include<stdio.h>
int main()
{
int empno=0,tot;
float hours, otp, prcn;
while(empno!=-999){
printf("Enter the Employee No: ");
scanf("%d",&empno);
printf("Enter the No of Hours: ");
scanf("%f",&hours);
if(hours<40){
otp=hours*150.00;
}
else
otp=((hours-40)*200)+(hours*150.00);
if(otp>4000){
prcn+=1;
tot+=1;
}
else
tot+=1;
printf("\nEmployee No: %d\n",empno);
printf("Over Time Payment: %.2f\n",otp);
printf("Percentage of Employees whose OTP exceeding the Rs. 4000/-: %.2f%%\n\n",(prcn/tot)*100);
}
}
我希望当我输入-999而不继续时它会停止。
之后没有声明:
printf("Enter the Employee No: ");
scanf("%d",&empno);
和之前:
printf("Enter the No of Hours: ");
scanf("%f",&hours);
输入-999
后,将导致程序停止。 while
循环检查在您开始时会进行一次(在您输入第一个员工编号之前),但是直到您完成所有数据输入,计算和打印之后才会再次进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.