我编写的代码:
#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/