【发布时间】: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' && yos < 5)而不是else if (char time == 'P' && yos < 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