换行符在格式字符串中时 scanf 的行为 [英] Behaviour of scanf when newline is in the format string

查看:36
本文介绍了换行符在格式字符串中时 scanf 的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

推荐答案

scanf("%d ", &paycode格式字符串中的' '字符) 在给定的输入.因此,scanf 调用将读取并丢弃任意数量的空白字符,直到遇到非空白字符,此时它将返回.这适用于 scanf 格式字符串中的任何空白字符,而不仅仅是换行符.例如,以下将表现出相同的行为:

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

您应该将您的 scanf 调用更改为

You should change your scanf call to

scanf("%d", &paycode);

另外,您可以简单地编写 printf("Paycode: "); 而不是 printf("%s", "Paycode: ");您已经评论说 stdio.h 是一个 precompiled 标头.不是.它是一个包含宏定义和函数声明或原型的头文件.它不是预编译意义上的目标文件.

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.

这篇关于换行符在格式字符串中时 scanf 的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆