Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c - Behaviour of scanf when newline is in the format string

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ' ' character in the format string of scanf("%d ", &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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...