【问题标题】:Running into segmentation fault for below code遇到以下代码的分段错误
【发布时间】:2015-06-24 11:37:32
【问题描述】:

大家好,我在学校项目计划中需要帮助,它是 switch()、if()...else、if()...else if()... 和任何其他构造的组合你需要有正确的输出。

程序规格:

某公司加薪如下:

技术人员:工作10年以上者加5%。 3% 给其他人

行政人员:

  • 6%为会计部人员
  • 4% 为市场部人员
  • 3% 其他
其他员工:增加 2%

这是我的代码,但每次我编译并运行它都会出现运行时错误。:希望有人能帮助我,因为我是编程新手,明天到期

#include <stdio.h>
#include <conio.h>

int main (void) 
{
int currentsalary;
int yearsinservice;
int percent;
float newsalary;
char choice1 ,choice2;



printf("Current Salary: \n");
scanf("%d", &currentsalary);
printf("Staff Category: \n");
printf("T - technical \n");
printf("A - administrative \n");
printf("O - others \n");
printf("Enter the staff Category: \n");
scanf(" %c", choice1);

switch (choice1)
{
       case't':
       case'T': printf("No. of years in Service: ");
                scanf("%d", &yearsinservice);
            if (yearsinservice > 10)
{
                     percent = currentsalary*.05;
                     newsalary = currentsalary + percent;
                     printf("New Salary: %.2f", newsalary);
}
else
{
    percent = currentsalary*.03;
    newsalary = currentsalary + percent;
    printf("New Salary: %.2F", newsalary);
}
    break;

case'a':
case'A': printf("Department where the staff belongs:");
        printf("A - accounting\n");          
        printf("M - marketing\n");
        printf("O - others\n");
        printf("Choose the department:\n");
        scanf(" %c",&choice2);
               if (choice2 == 'a' || choice2 == 'A')
               {
                              percent = currentsalary*.06;
                              newsalary = currentsalary+percent;
                              printf("New Salary: %.2f", newsalary);
               }
               else if (choice2 == 'm' || choice2 == 'M');
               {
                    percent = currentsalary*.04;
                    newsalary = currentsalary+percent;
                    printf("New Salary: %.2f", newsalary);
               }
               else 
               {
                   percent = currentsalary*.03;
                   newsalary = salary+percent;
                   printf("New Salary; %.2f", newsalary);

               }

               break;
               case'o':
               case'O': percent = currentsalary*.02;
               newsalary = currentsalary + percent;
               printf("New Salary: %.2f",newsalary);
               break;
               default: printf("Wrong Input");
               getch();
               }    
               getch();   
               return 0;
               }



示例输出 1 中的结果应该相同:

目前工资:15000
员工类别:
T - 技术
A - 行政
O - 其他
输入员工类别:T
没有。服务年限:11
新工资:15750.00



输出2:

目前工资:20000
员工类别:
T - 技术
A - 行政
O - 其他
输入员工类别:A
员工所属部门
A - 会计
M - 市场营销
O - 其他
选择部门:A
新工资:21200.00



输出3:

目前工资:15000
员工类别:
T - 技术
A - 行政
O - 其他
输入员工类别:O


新工资:15300.00




【问题讨论】:

  • 什么是运行时错误?
  • 我在“else”之前出现错误“语法错误”
  • 什么时候语法错误是运行时错误?
  • 如果代码缩进正确,它可能会帮助您找出语法错误。

标签: c if-statement segmentation-fault scanf dev-c++


【解决方案1】:

如我所见

第 1 点:(您的 runtime 错误)

 scanf(" %c", choice1);

应该是

scanf(" %c", &choice1);
             ^
             |
        notice here

第 2 点:(您的语法错误)

caseA: 块中的else if 条件之后,您有一个不需要的;。删除它。

【讨论】:

  • @Andrew_Gramming 是的。在情况 a 中,删除 ;在 else if 语句之后。
【解决方案2】:

这行有问题

else if (choice2 == 'm' || choice2 == 'M');
                                          ^

去掉末尾的分号;

【讨论】:

    【解决方案3】:

    您的第一个错误是您正在调用

    scanf(" %c", choice1);
    

    什么时候打电话

    scanf(" %c", &choice1);
                 ^
                 need the & here
    

    您应该在变量之前添加&amp;(字符串除外)。

    你的下一个错误是

    else if (choice2 == 'm' || choice2 == 'M');
                                              ^
                                             semicolon not needed
    

    你在最后留下了一个分号。删除它。

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 2020-06-15
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      相关资源
      最近更新 更多