c - 为什么当我第二次运行循环时该程序会崩溃?

标签 c

尽管我尽力解开这个谜题,但我想我已经疯了!谁能帮助理解为什么当我第二次运行循环时该程序崩溃?

我可以运行一次交互式循环并将输入的值写入文件。然而,当我尝试第二次通过 a 循环时,程序卡住了。

// C Libraries Used

#include <stdio.h>
#include <math.h>
#include <string.h>

// Constant definitions

const float OTPAYFACTOR = 1.5;
const float REGWORKWKHRS = 40;
FILE *payfile;                // report file (for output)

// Variable declerations
char deptname [21];
char firstname [10];
char lastname [10];
char fullname [47];
float hrsworked;
float hrwage;
float reghrsworked;
float othrsworked;
float otwage;
float grosswage;
int count;
char again;

// Function Prototypes

//**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~
// M A I N   F U N C T I O N
//**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~

int main (void){
    payfile = fopen("c:\\class\\mod6\\ethan-pay.txt","w");     // Open disk file
    printf("Mountain Pacific Corporation\nDepartment Salary Program\n\n");
    printf("Please enter the name of the department: ");
    scanf("%s", deptname);
    count = 0;    // Initialize this "counting" variable to zero to start
    printf("%s", deptname);
    printf("\n");
do {

      printf("Enter employee #%d: ", count+1);
      scanf("%s %s", firstname, lastname);
      fscanf(payfile,"%s %s", firstname, lastname);
      strcpy(fullname, firstname);
      strcat(fullname, " ");
      strcat(fullname, lastname);
      printf("Enter the hourly wage of %s: ", fullname);
      scanf("%f", &hrwage);
      fscanf(payfile,"%f", &hrwage);
      printf("Enter total number of hours: ");
      scanf("%f", &hrsworked);
      fscanf(payfile,"%f", &hrsworked);

    if (hrsworked <= REGWORKWKHRS){     //
        reghrsworked = hrsworked;
        othrsworked = 0;
        otwage = hrwage * OTPAYFACTOR;
        grosswage = hrwage*reghrsworked;
    }
        else{

            reghrsworked = REGWORKWKHRS;
            othrsworked = hrsworked - REGWORKWKHRS;
            otwage = hrwage * OTPAYFACTOR;
            grosswage = (reghrsworked * hrwage) + (othrsworked * otwage);
        }
       fprintf(payfile,"%-22s%0.1f ($%0.2f) %6.1f ($%0.2f) $%-4.2f\n", fullname, reghrsworked, hrwage, othrsworked, otwage, grosswage);
       printf("\nThank you. Process another employee? ");
       scanf ("%s", &again);
       printf("\n");

       count++; // Increment the counting variable


} while (again == 'Y'|| again == 'y' || again != 'N' && again != 'n');


      printf("End of processing.\n");

    fclose(payfile);
return 0;
}

最佳答案

我不明白您为什么要扫描用于保存结果的文件:

scanf("%s %s", firstname, lastname);
fscanf(payfile,"%s %s", firstname, lastname);
// ...
scanf("%f", &hrwage);
fscanf(payfile,"%f", &hrwage);
// ...
scanf("%f", &hrsworked);
fscanf(payfile,"%f", &hrsworked);

你所做的删除了之前的值。只需删除对 fscanf() 的所有调用即可。

我强烈建议你改变你的代码实践,应该避免全局,只在需要时声明变量,不要忽略函数中的错误代码,提供给scanf()它可以使用的最大尺寸等...

示例可能并不完美:

#include <stdio.h>
#include <string.h>

#define OTPAYFACTOR 1.5
#define REGWORKWKHRS 40

int main(void) {
  printf("Mountain Pacific Corporation\nDepartment Salary Program\n\n"
         "Please enter the name of the department: ");
  char deptname[21];
  if (scanf("%20s", deptname) != 1) {
    return 1;
  }
  printf("%s\n", deptname);

  FILE *payfile = fopen("c:\\class\\mod6\\ethan-pay.txt", "w");
  if (!payfile) {
    return 1;
  }
  for (int count = 0;; count++) {
    printf("Enter employee #%d: ", count + 1);
    char firstname[10];
    char lastname[10];
    if (scanf("%9s %9s", firstname, lastname) != 2) {
      return 1;
    }
    char fullname[19];
    sprintf(fullname, "%s %s", firstname, lastname);
    printf("Enter the hourly wage of %s: ", fullname);
    float hrwage;
    if (scanf("%f", &hrwage) != 1) {
      return 1;
    }
    printf("Enter total number of hours: ");
    float hrsworked;
    if (scanf("%f", &hrsworked) != 1) {
      return 1;
    }
    if (hrsworked <= REGWORKWKHRS) {
      float reghrsworked = hrsworked;
      float othrsworked = 0;
      float otwage = hrwage * OTPAYFACTOR;
      float grosswage = hrwage * reghrsworked;
      fprintf(stdout, "%-22s%0.1f ($%0.2f) %6.1f ($%0.2f) $%-4.2f\n", fullname,
              reghrsworked, hrwage, othrsworked, otwage, grosswage);
    } else {
      float reghrsworked = REGWORKWKHRS;
      float othrsworked = hrsworked - REGWORKWKHRS;
      float otwage = hrwage * OTPAYFACTOR;
      float grosswage = (reghrsworked * hrwage) + (othrsworked * otwage);
      fprintf(stdout, "%-22s%0.1f ($%0.2f) %6.1f ($%0.2f) $%-4.2f\n", fullname,
              reghrsworked, hrwage, othrsworked, otwage, grosswage);
    }

    printf("\nThank you. Process another employee? ");
    char again;
    if (scanf(" %c", &again) != 1) {
      return 1;
    }
    if (again != 'Y' && again != 'y') {
      break;
    }
    printf("\n");
  }
  fclose(payfile);
  printf("End of processing.\n");
}

示例输入:

jurasickpark
sarah connor 5 42
y
bob lennon 9 12
n

输出文件:

sarah connor          40.0 ($5.00)    2.0 ($7.50) $215.00
bob lennon            12.0 ($9.00)    0.0 ($13.50) $108.00

关于c - 为什么当我第二次运行循环时该程序会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47252552/

相关文章:

c - 如何获取发送IRP请求的模块

c - 当 i 已经用 0 和 if 条件初始化时,i 的值是多少?为什么?

c - SCHED_IDLE 是否实际上排除了在非空闲内核上的执行?

c - 删除 libxml2 c 中 xmldocument 和根节点之间的空白

objective-c - Objective C 结构语法

c - rand() 函数在选择 IP 时具有概率

c - 并行运行 while 循环

c - 如何使用级数 sinx = x - (x3/3) 计算三角比! + (x5/5)i - (x7/7)! + 一个 C 程序

使用字符串数组调用整数的 1's, 10' s, 100's... 列

c - 在 C 语言中,如何将单个变量设为 're-initialize' 并在 while 循环的每次迭代中使用它?