Scanf behavior when newline is in the format string

advertisements

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.\n");
    printf("1: Manager\n");
    printf("2: Hourly Worker\n");
    printf("3: Commission Worker\n");
    printf("4: Pieceworker\n");
    printf("-1 to end\n");
    printf("%s","Paycode: ");
    scanf("%d\n", &paycode);

    while (paycode != -1)//begin while loop
    {
        switch(paycode)
        {
        case 1: //calculate manager's pay
            printf("Manager selected.\n");
            printf("Enter weekly salary: $ ");
            scanf("%f", &salary);
            counter = counter + 1;
            printf("Weekly salary is %.2f\n\n", salary);
            break;
        case 2:
            printf("Hourly worker selected.\n");
            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\n", overtime);
            }
            counter2 = counter2 +1;
            printf("Weekly salary is: $%.2f\n\n", salary);
            break;
        case 3:
            printf("Commissioned worker selected.\n");
            printf("Enter gross weekly sales: $");
            scanf("%f", &grossWeeklySales);
            salary=.057*grossWeeklySales+250;
            counter3 = counter3 +1;
            printf("Weekly salary is: $%.2f\n\n", salary);
            break;
        case 4:
            printf("Pieceworker Selected.\n");
            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\n\n", salary);
        }
        //get next input
        printf("Please enter paycode, -1 to end.\n");
        printf("%s","Paycode: ");
        scanf("%d", &paycode);
    }
    printf("Number of managers paid: %d\n", counter); //display amount of workers paid
    printf("Number of hourly workers paid is: %d\n", counter2);
    printf("Number of commisioned workers is: %d\n", counter3);
    printf("Number of piece workers paid is: %d\n\n", counter4);
}


The '\n' character in the format string of scanf("%d\n", &paycode) matches any number of whitespace characters (space, tab, newline etc. - characters for which the isspace function declared in ctype.h yields true) in the input given. Therefore, the scanf call will read and discard any number of whitespace characters till it encounters a non-whitespace character at which point it will return. This is true for any whitespace character in the format string of scanf and not only the newline character. For example, the following will exhibit the same behaviour:

scanf("%d ", &paycode)
         ^ a space

You should change your scanf call to

scanf("%d", &paycode);

Also, instead of printf("%s", "Paycode: "); you can simply write printf("Paycode: "); You have commented that stdio.h is a precompiled header. It's not. It's a header file which contains macro definitions and function declarations or prototypes. It's not an object file in the sense of being precompiled.