使用If语句扫描char和Combining Switch语句?开关(时间)

时间:2016-06-25 12:00:34

标签: c if-statement char switch-statement output

我写过的代码:

#include<stdio.h>
int main()
{
    int yos;
    double salary;
    char time;

    printf("Please enter your employee status, 'P' for Fulltime and 'P' for Parttime: \n");
    scanf_s("%c", &time);
    printf("Please enter your year of service: \n");
    scanf_s("%d", &yos);
    printf("Please enter your current salary: \n");
    scanf_s("%lf", &salary);

    switch (time)
    {
    case 'F':
    case 'f':
        if (yos >= 5)
        {
            salary = (salary*5.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        else if (yos < 5 )
        {
            salary = (salary*4.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);   
        }
        break;

    case 'P':
    case 'p':
        if (yos >= 5)
        {
            salary = (salary*3.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        else if (yos < 5 )
        {
            salary = (salary*2.5 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);       
        }
        break;
    default:
        printf("Please put the details correctly\n");
    }

    return(0);
}

出于某种原因,当我运行程序时,我得到了这个输出:

Please enter your employee status, 'P' for Fulltime and 'P' for Parttime:
F
Please enter your year of service:
6
Please enter your current salary:
200
Please put the details correctly
Press any key to continue

是否会出现此问题,因为它无法扫描char?我甚至试过间隔%c。我也不认为放%s或%[^ \ n]会有任何用处,因为它只涉及1个字符。请有人帮帮我吗?

我还尝试过不同的代码,只涉及以下语句:

#include<stdio.h>
int main()
{
    int yos;
    double salary;
    char time;

    printf("Please enter your employee status, 'P' for Fulltime and 'P' for Parttime: \n");
    scanf_s("%c", &time);
    printf("Please enter your year of service: \n");
    scanf_s("%d", &yos);
    printf("Please enter your current salary: \n");
    scanf_s("%lf", &salary);

    if (char time == 'F' && yos >= 5)
    {
        salary = (salary*5.0 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }

    else if (char time == 'F' && yos < 5)
    {
        salary = (salary*4.0 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }

    if (char time == 'P' && yos >= 5)
    {
        salary = (salary*3.0 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }

    else if (char time == 'P' && yos < 5)
    {
        salary == (salary*2.5 / 100.0) + salary;
        printf("\nYour new salary is %.2lf", salary);
    }
    return(0);
}

但是,这个是给予

error c2143 missing ',' before '==' at line 15, 21, 27...

还有:

Warning 11  warning C4553: '==' : operator has no effect; did you intend '='?   

3 个答案:

答案 0 :(得分:0)

我选择使用scanf而不是scanf_s,我更喜欢在switch语句中使用枚举和整数。以下编辑代码对我有用;

#include<stdio.h>
int main()
{
    int yos;
    double salary;
    char time;

    printf("Please enter your employee status, 'F' for Fulltime and 'P' for Parttime: \n");
    scanf("%c", &time);
    printf("Please enter your year of service: \n");
    scanf("%d", &yos);
    printf("Please enter your current salary: \n");
    scanf("%lf", &salary);

    int status;
    if(time=='F'||time=='f')
        status = 1;
    if(time=='P'||time=='p')
        status = 2;
    switch (status)
    {
    case 1:
        if (yos >= 5)
        {
            salary = (salary*5.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        else if (yos < 5 )
        {
            salary = (salary*4.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        break;

    case 2:
        if (yos >= 5)
        {
            salary = (salary*3.0 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        else if (yos < 5 )
        {
            salary = (salary*2.5 / 100.0) + salary;
            printf("\nYour new salary is %.2lf", salary);
        }
        break;
    default:
        printf("Please put the details correctly\n");
    }

    return(0);
}

答案 1 :(得分:0)

使用%c格式说明符时,需要提供一个附加参数,指定要读取的字符数。来自MSDN page for scanf_s

  

scanfwscanf不同,scanf_swscanf_s需要缓冲区大小   要为c,C,s,S或string类型的所有输入参数指定   包含在[]中的控件集。缓冲区大小以字符为单位   在指针指向后立即作为附加参数传递   缓冲区或变量。

     

...

     

对于字符,可以按如下方式读取单个字符:

char c;

scanf_s("%c", &c, 1);

因为您没有传递所需数量的参数,所以您可以调用undefined behavior

因此,当您在time中阅读时,请执行以下操作:

scanf_s("%c", &time, 1);

此外,在您的第二个示例中,这是无效的语法:

if (char time == 'F' && yos >= 5)

在此处删除char关键字。

答案 2 :(得分:-1)

scanf_s是否特定于Microsoft,您是否必须使用它?如果我尝试使用scanf编译器,那么程序的第一个版本似乎可以工作,并且在程序运行时不会打印默认语句。

#include<stdio.h>

int main() {
    int yos;
    double salary;
    char time;

    printf("Please enter your employee status, 'F' for Fulltime and 'P' for Parttime: \n");
    scanf("%c", &time);
    printf("Please enter your year of service: \n");
    scanf("%d", &yos);
    printf("Please enter your current salary: \n");
    scanf("%lf", &salary);

    switch (time) {
        case 'F':
        case 'f':
            if (yos >= 5) {
                salary = (salary * 5.0 / 100.0) + salary;
                printf("\nYour new salary is %.2lf", salary);
            }
            else if (yos < 5) {
                salary = (salary * 4.0 / 100.0) + salary;
                printf("\nYour new salary is %.2lf", salary);
            }
            break;

        case 'P':
        case 'p':
            if (yos >= 5) {
                salary = (salary * 3.0 / 100.0) + salary;
                printf("\nYour new salary is %.2lf", salary);
            }
            else if (yos < 5) {
                salary = (salary * 2.5 / 100.0) + salary;
                printf("\nYour new salary is %.2lf", salary);
            }
            break;
        default:
            printf("Please put the details correctly\n");
    }

    return (0);
} 

测试

Please enter your employee status, 'F' for Fulltime and 'P' for Parttime: 
F
Please enter your year of service: 
6
Please enter your current salary: 
200

Your new salary is 210.00