簡體   English   中英

如何在鍵盤輸入時立即停止C while循環而不進行下一步?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM