当换行符位于格式字符串中时 scanf 的行为

use*_*251 4 c counter while-loop switch-statement

下面是我的代码的副本。基本上,我需要创建一个程序,根据“工资代码”(例如工人的职位)计算工资。我已经创建了我的 switch 语句,除了我输入第一个付款码时的一开始,一切正常。我输入第一个付款代码,它会转到下一行,将其留空。我输入另一个数字,它会按照预期的方式运行该数字和前一个数字。然后,一切正常。我确信这是一个简单的解决方案,但对我来说有点棘手。另外,我不确定应该如何在这里格式化我的代码,使其看起来像在服务器上一样,所以我对它看起来令人困惑的方式表示歉意。

#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);
}
Run Code Online (Sandbox Code Playgroud)

aja*_*jay 6

'\n'格式字符串中的字符与给定输入中scanf("%d\n", &paycode)任意数量的空白字符(空格、制表符、换行符等 - 中isspace声明的函数产生true 的字符)相匹配。ctype.h因此,该scanf调用将读取并丢弃任意数量的空白字符,直到遇到非空白字符,此时它将返回。对于格式字符串中的任何空白字符都是如此scanf,而不仅仅是换行符。例如,以下内容将表现出相同的行为:

scanf("%d ", &paycode)
         ^ a space
Run Code Online (Sandbox Code Playgroud)

您应该将您的scanf电话更改为

scanf("%d", &paycode);
Run Code Online (Sandbox Code Playgroud)

另外, printf("%s", "Paycode: "); 您也可以简单地写成printf("Paycode: "); You have commented that stdio.his a precompiled header。它不是。它是一个包含宏定义和函数声明或原型的头文件。它不是预编译意义上的目标文件。