【问题标题】:Scanning char and Combining Switch statement with If statement? switch(time)扫描字符并将 Switch 语句与 If 语句相结合?开关(时间)
【发布时间】:2016-10-27 22:51:52
【问题描述】:

我写的代码:

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

【问题讨论】:

  • 'P' for Fulltime and 'P' for Parttime....什么?
  • 在您的代码的第二个版本中,它应该是 else if (time == 'P' &amp;&amp; yos &lt; 5) 而不是 else if (char time == 'P' &amp;&amp; yos &lt; 5)。注意多余的词char
  • 你想阅读文档到scanf_s(): msdn.microsoft.com/en-us/library/w40768et.aspx ("不像scanf [...] scanf_s [...] 需要[s] 缓冲区大小到被指定为 ..."
  • 您应该确定您的问题是关于编写 C 代码还是 Microsoft C 代码。在每种情况下,您都应该正确标记您的问题。 scanf_s 不是 C 函数,而是 Microsoft 扩展。

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


【解决方案1】:

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

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

...

在字符的情况下,单个字符可以如下读取:

char c;

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

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

所以当你在time阅读时,这样做:

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

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

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

在这里去掉char关键字。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      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
      

      【讨论】:

      • ive 改变了前任的设置,甚至尝试了#define _CRT_SECURE_NO_WARNINGS,甚至两者都做了。出于安全原因,不能真正使用 scanf。顺便感谢所有回复的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多