为什么我的程序不打印开关盒后面的代码?它在执行switch case之后结束,就是这样
如果我隐藏开关盒,代码可以正常工作。如果我为开关案例输入无效int,它将在开关案例之后执行printf语句。如果我为switch case输入有效的int,它将进入switch case并在那里结束,然后跳过printf语句。看起来不错,但显然不是为什么我的程序不打印开关盒后面的代码?它在执行switch case之后结束,就是这样,c,switch-statement,C,Switch Statement,如果我隐藏开关盒,代码可以正常工作。如果我为开关案例输入无效int,它将在开关案例之后执行printf语句。如果我为switch case输入有效的int,它将进入switch case并在那里结束,然后跳过printf语句。看起来不错,但显然不是 #include<stdio.h> #include<string.h> #define OT 14.00 void main () { char name[40]; char position[20];
#include<stdio.h>
#include<string.h>
#define OT 14.00
void main ()
{
char name[40];
char position[20];
char position_name[20];
int id, o_time, code;
float allowance, bonus, nett_salary, salary;
printf ("-----------------------------------");
printf ("\n PAYROLL SYSTEM");
printf ("\n-----------------------------------");
printf ("\n 1. Lecturer");
printf ("\n 2. Clerk");
printf ("\n 3. Technician");
printf ("\n-----------------------------------");
printf ("\nEnter staff name : ");
scanf("%[^\n]s",&name);
printf ("Enter staff ID : ");
scanf ("%d", &id);
printf ("Enter position : ");
scanf ("%d", &code);
printf ("Enter salary : RM ");
scanf ("%f", &salary);
switch (code)
{
case 1:
strcpy (position, "Lecturer");
printf ("Enter allowance : RM ");
scanf ("%f", allowance);
nett_salary = salary+allowance;
break;
case 2:
strcpy (position, "Clerk");
printf ("Enter bonus : RM ");
scanf ("%f", bonus);
nett_salary = salary+bonus;
break;
case 3:
strcpy (position, "Technician");
printf ("Enter overtime hours : ");
scanf ("%d", o_time);
nett_salary = salary+(o_time*OT);
break;
default:
strcpy(position,"Invalid");
nett_salary = 0;
}
printf ("\nStaff Name : %s", name);
printf ("\nStaff No. : %d", id);
printf ("\nPosition : %s", position);
printf ("\nSalary : RM%.2f", salary);
printf ("\n------------------------------------");
printf ("\nNett Salary : RM %.2f", nett_salary);
printf ("\n------------------------------------");
}
#包括
#包括
#定义OT 14.00
空干管()
{
字符名[40];
字符位置[20];
字符位置_名称[20];
int id,o_时间,代码;
浮动津贴、奖金、净工资、薪金;
printf(“-----------------------------------------”;
printf(“\n工资系统”);
printf(“\n-----------------------------------------”;
printf(“\n 1.讲师”);
printf(“\n 2.职员”);
printf(“\n 3.技术人员”);
printf(“\n-----------------------------------------”;
printf(“\n输入员工姓名:”);
scanf(“%[^\n]s”、&name);
printf(“输入员工ID:”);
scanf(“%d”和&id);
printf(“输入位置:”);
scanf(“%d”、&code);
printf(“输入工资:RM”);
scanf(“%f”和工资);
开关(代码)
{
案例1:
strcpy(职位,“讲师”);
printf(“输入津贴:RM”);
scanf(“%f”,余量);
净工资=工资+津贴;
打破
案例2:
strcpy(职位,“文员”);
printf(“输入奖金:RM”);
scanf(“%f”,奖金);
净工资=工资+奖金;
打破
案例3:
strcpy(职位,“技术员”);
printf(“输入加班时间:”);
扫描频率(“%d”,o_时间);
净工资=工资+(加班时间*OT);
打破
违约:
strcpy(位置,“无效”);
净工资=0;
}
printf(“\n工作人员名称:%s”,名称);
printf(“\n员工编号:%d”,id);
printf(“\n位置:%s”,位置);
printf(“\n工资:RM%.2f”,工资);
printf(“\n--------------------------------------------------------”;
printf(“\n净工资:RM%.2f”,净工资);
printf(“\n--------------------------------------------------------”;
}
主要问题在于
scanf ("%f", allowance);
其中提供的参数缺少&
运算符,所有case语句都是如此
也就是说,不要对用户输入使用scanf()
,它有许多缺点。改用fgets()
scanf ("%f", allowance); // In case 1
scanf ("%f", bonus); // In case 2
scanf ("%d", o_time); // In case 3
这些
scanf
语句中缺少&
。您可以使用此字符&将变量放入其地址中
#include<stdio.h>
#include<string.h>
#define OT 14.00
void main ()
{
char name[40];
char position[20];
char position_name[20];
int id=0, o_time=0, code=0;
float allowance, bonus, nett_salary, salary;
printf ("-----------------------------------");
printf ("\n PAYROLL SYSTEM");
printf ("\n-----------------------------------");
printf ("\n 1. Lecturer");
printf ("\n 2. Clerk");
printf ("\n 3. Technician");
printf ("\n-----------------------------------");
printf ("\nEnter staff name : ");
fgets(name,40,stdin);//use fgets insted of scanf
printf ("Enter staff ID : ");
scanf ("%d", &id);
printf ("Enter position : ");
scanf ("%d", &code);
printf ("Enter salary : RM ");
scanf ("%f", &salary);
switch (code)
{
case 1:
strcpy (position, "Lecturer");
printf ("Enter allowance : RM ");
scanf ("%f",&allowance);//you forget &
nett_salary = (float)salary+allowance;
break;
case 2:
strcpy (position, "Clerk");
printf ("Enter bonus : RM ");
scanf ("%f", &bonus);//you forget &
nett_salary = salary+bonus;
break;
case 3:
strcpy (position, "Technician");
printf ("Enter overtime hours : ");
scanf ("%d", &o_time);//you forget &
nett_salary = salary+(o_time*OT);
break;
default:
strcpy(position,"Invalid");
nett_salary = 0;
break;
}
printf ("\nStaff Name : %s", name);
printf ("\nStaff No. : %d", id);
printf ("\nPosition : %s", position);
printf ("\nSalary : RM%.2f", salary);
printf ("\n------------------------------------");
printf ("\nNett Salary : RM %.2f", nett_salary);
printf ("\n------------------------------------");
}
#包括
#包括
#定义OT 14.00
空干管()
{
字符名[40];
字符位置[20];
字符位置_名称[20];
int id=0,o_时间=0,代码=0;
浮动津贴、奖金、净工资、薪金;
printf(“-----------------------------------------”;
printf(“\n工资系统”);
printf(“\n-----------------------------------------”;
printf(“\n 1.讲师”);
printf(“\n 2.职员”);
printf(“\n 3.技术人员”);
printf(“\n-----------------------------------------”;
printf(“\n输入员工姓名:”);
fgets(name,40,stdin);//使用fgets代替scanf
printf(“输入员工ID:”);
scanf(“%d”和&id);
printf(“输入位置:”);
scanf(“%d”、&code);
printf(“输入工资:RM”);
scanf(“%f”和工资);
开关(代码)
{
案例1:
strcpy(职位,“讲师”);
printf(“输入津贴:RM”);
scanf(“%f”、&amount);//您忘记了吗&
净工资=(浮动)工资+津贴;
打破
案例2:
strcpy(职位,“文员”);
printf(“输入奖金:RM”);
scanf(“%f”,&bonus);//您忘记了吗&
净工资=工资+奖金;
打破
案例3:
strcpy(职位,“技术员”);
printf(“输入加班时间:”);
scanf(“%d”,&o_time);//您忘记了吗&
净工资=工资+(加班时间*OT);
打破
违约:
strcpy(位置,“无效”);
净工资=0;
打破
}
printf(“\n工作人员名称:%s”,名称);
printf(“\n员工编号:%d”,id);
printf(“\n位置:%s”,位置);
printf(“\n工资:RM%.2f”,工资);
printf(“\n--------------------------------------------------------”;
printf(“\n净工资:RM%.2f”,净工资);
printf(“\n--------------------------------------------------------”;
}
scanf(“%[^\n]s”&名称)代码>不好…您应该使用fgets()
。好的,这很有效!非常感谢你,谢谢!这个有效!请注意,我将使用fgetsyes,谢谢!成功了!