c - 扫描 char 并将 Switch 语句与 If 语句组合?开关(时间)

标签 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

这个问题是因为无法扫描字符而出现的吗?我什至尝试将 %c 隔开。我也不认为放置 %s 或 %[^\n] 有任何用处,因为它只涉及 1 个字符。请有人帮助我吗?

我还尝试了仅涉及 if 语句的不同代码,例如:

#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 '='?   

最佳答案

我选择使用 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);
}

关于c - 扫描 char 并将 Switch 语句与 If 语句组合?开关(时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38028295/

相关文章:

Java,方法打印 if/else 中的两个选项

c - 带有嵌套函数的 if 语句将不会执行

java - 字符串的插入排序

c - 在 C 中使用数组 [100] 时出现运行时错误

c - 为什么我的代码中的第二个 strcpy() 导致停止工作?

c - 链表只抓取文件内容的最后输入

c - 如何跳出嵌套循环?

c# - 从 C# 调用 C# : How to marshall that one 2D array of doubles when the others go so well?

mysql - 警告消息不显示

将指针转换为 _Atomic 指针和 _Atomic 大小