Ajini Sahejana
Ajini Sahejana

Reputation: 1

How to stop a C while loop at once when we keyboard input without going on to the next step?

After I run this program, it has to stop when I input -999. But it stops even after the execution of -999. What have I done wrong here?

This was the question for the code.

Input employee number, and hours worked by employees, and to display the following: Employee number, Over Time Payment, and the percentage of employees whose Over Time Payment exceeding the Rs. 4000/-. The user should input –999 as employee number to end the program, and the normal Over Time Rate is Rs.150 per hour and Rs. 200 per hour for hours in excess of 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);
    }
}

I expect it to stop when I enter -999 without going on.

Upvotes: 0

Views: 394

Answers (1)

Dave M.
Dave M.

Reputation: 1537

There is no statement after:

    printf("Enter the Employee No: ");
    scanf("%d",&empno);

and before:

    printf("Enter the No of Hours: ");
    scanf("%f",&hours);

that would cause your program to stop after you enter -999. The while loop check happens once when you start (before you enter the first employee number) but it doesn't happen again until after you do all the data entry, calculation and printing.

Upvotes: 1

Related Questions