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

时间:2014-02-18 16:13:04

标签: c while-loop switch-statement counter

以下是我的代码的副本。基本上,我需要创建一个程序,根据“工资代码”计算工资,例如工人的职位。我已经创建了我的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);
}

5 个答案:

答案 0 :(得分:3)

函数scanf作为参数接收您期望的格式。通过scanf("%d\n", &paycode);,您说“嘿,计算机,读取数字和换行符,并将其保存在paycode变量中”。此外,scanf将读取并忽略空白字符。

尝试将其更改为scanf("%d", &paycode);,以便计算机只读取并保存该号码。

答案 1 :(得分:3)

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

scanf

您应该将scanf("%d ", &paycode) ^ a space 来电更改为

scanf

此外,您可以简单地写scanf("%d", &paycode); 而不是printf("%s", "Paycode: ");。 您评论说printf("Paycode: ");预编译的标头。不是。它是一个头文件,包含宏定义和函数声明或原型。它不是预编译意义上的目标文件。

答案 2 :(得分:0)

更改

scanf("%d\n", &paycode);

scanf("%d", &paycode);

答案 3 :(得分:0)

scanf("%d\n", &paycode);
         ^^ Scanf is waiting for this newline to come.

将其更改为:

scanf("%d", &paycode);

答案 4 :(得分:0)

换行常数令人困惑。你需要改变第一个

scanf(“%d \ n”,&amp; paycode);

scanf(“%d”,&amp; paycode);