Below is the copy of my code. Basically, I need to create a program that calculates pay based on "paycodes" eg the worker's position. I've created my switch statement and everything works except for the very beginning when I'm entering the first paycode. I enter the first paycode, it goes to the next line, leaving it blank. I put in another number, and it runs that number and the previous number the way it is supposed to. Then, after that everything works fine. I'm sure it's a simple solution but it is being a bit tricky for me. Also, I'm not sure exactly how I'm supposed to format my code on here to make it look like it does on the server, so I apologize for the confusing way it looks.
#include <stdio.h> //precompiled header
int main(void)
{
//declare variables
unsigned int counter = 0; //counters to calculate amount of workers paid
unsigned int counter2 = 0;
unsigned int counter3 = 0;
unsigned int counter4 = 0;
int paycode;
float overtime; //amount of overtime hours
float salary; //weekly salary
float hoursWorked;
float hourlyRate;
float grossWeeklySales; //weekly sales for commissioned workers
int itemsProduced;
float fixedAmount; //money given per item produced
//prompt for input
printf("Please enter the employee's paycode.
");
printf("1: Manager
");
printf("2: Hourly Worker
");
printf("3: Commission Worker
");
printf("4: Pieceworker
");
printf("-1 to end
");
printf("%s","Paycode: ");
scanf("%d
", &paycode);
while (paycode != -1)//begin while loop
{
switch(paycode)
{
case 1: //calculate manager's pay
printf("Manager selected.
");
printf("Enter weekly salary: $ ");
scanf("%f", &salary);
counter = counter + 1;
printf("Weekly salary is %.2f
", salary);
break;
case 2:
printf("Hourly worker selected.
");
printf("Enter hourly rate: $");
scanf("%f", &hourlyRate);
printf("Enter hours worked: ");
scanf("%f", &hoursWorked);
if(hoursWorked<=40) //if statement to calculate overtime
{
salary=hourlyRate*hoursWorked;
printf("No overtime worked.");
}
else
{
salary=40.0*hourlyRate+(hoursWorked-40)*1.5*hourlyRate;
overtime = hoursWorked - 40;
printf("Total amount of overtime worked: %.2f
", overtime);
}
counter2 = counter2 +1;
printf("Weekly salary is: $%.2f
", salary);
break;
case 3:
printf("Commissioned worker selected.
");
printf("Enter gross weekly sales: $");
scanf("%f", &grossWeeklySales);
salary=.057*grossWeeklySales+250;
counter3 = counter3 +1;
printf("Weekly salary is: $%.2f
", salary);
break;
case 4:
printf("Pieceworker Selected.
");
printf("Enter amount of items produced: ");
scanf("%d", &itemsProduced);
printf("Enter the fixed pay per item produced: $ ");
scanf("%f", &fixedAmount);
salary=itemsProduced*fixedAmount;
counter4 = counter4 + 1;
printf("Weekly salary is: $%.2f
", salary);
}
//get next input
printf("Please enter paycode, -1 to end.
");
printf("%s","Paycode: ");
scanf("%d", &paycode);
}
printf("Number of managers paid: %d
", counter); //display amount of workers paid
printf("Number of hourly workers paid is: %d
", counter2);
printf("Number of commisioned workers is: %d
", counter3);
printf("Number of piece workers paid is: %d
", counter4);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…