C 当我第二次运行循环时,为什么这个程序会崩溃?

C 当我第二次运行循环时,为什么这个程序会崩溃?,c,C,尽管我尽力解开这个谜,我想我是疯了!有人能帮助我理解为什么当我第二次运行循环时这个程序会崩溃吗 我可以运行一次交互式循环,并将输入的值写入文件。然而,当我第二次尝试通过a循环时,程序阻塞了 // C Libraries Used #include <stdio.h> #include <math.h> #include <string.h> // Constant definitions const float OTPAYFACTOR = 1.5; co

尽管我尽力解开这个谜,我想我是疯了!有人能帮助我理解为什么当我第二次运行循环时这个程序会崩溃吗

我可以运行一次交互式循环,并将输入的值写入文件。然而,当我第二次尝试通过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;
}
//使用的是C库
#包括
#包括
#包括
//恒定定义
常数浮动系数=1.5;
常量浮点REGWORKWKHRS=40;
文件*payfile;//报告文件(用于输出)
//可变偏差
char deptname[21];
charfirstname[10];
char lastname[10];
字符全名[47];
浮子被击碎;
浮动工资;
浮子倒装;
浮子工作;
浮动工资;
浮雕;
整数计数;
再次烧焦;
//功能原型
//**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~
//我是我的朋友
//**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~**~~
内部主(空){
payfile=fopen(“c:\\class\\mod6\\ethan pay.txt”,“w”);//打开磁盘文件
printf(“Mountain Pacific Corporation\n部门薪资计划”);
printf(“请输入部门名称:”);
scanf(“%s”,部门名称);
count=0;//将此“counting”变量初始化为零以启动
printf(“%s”,部门名称);
printf(“\n”);
做{
printf(“输入员工#%d:,计数+1);
scanf(“%s%s”,firstname,lastname);
fscanf(付款文件,“%s%s”,名字,姓氏);
strcpy(全名、名);
strcat(全名“”);
strcat(全名、姓氏);
printf(“输入%s的小时工资:”,全名);
scanf(“%f”、&hrwage);
fscanf(工资文件、%f、&hr工资);
printf(“输入总小时数:”);
scanf(“%f”和&hrsworked);
fscanf(付款文件、%f、&hrsworked);

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

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");
}
输出文件:

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

再次
是一个
字符
,因此您应该使用
scanf(“%c”,再次);
.
就足够了。调试器怎么说?请注意,您所有的变量都是全局变量,没有有效的原因,您应该始终检查您正在使用的
fopen
scanf
fscanf
的返回值,但我不明白为什么。您不应该将从stdin读取的数据写入文件?谢谢你的评论,告诉我这是主要功能,它对理解你的代码非常有用;)你说的“阻塞”是什么意思?错误消息?为什么要读取文件?firstname和lastname仅为10个字符,终止符限制为9个字符,fullname声明为长度47,这在很大程度上是未使用的,但您也可以将first/last单独输出到文件,而不是先复制到另一个缓冲区。您应该真正防止到处都有缓冲区溢出(strncpy与strcpy,另请参见)。
sarah connor          40.0 ($5.00)    2.0 ($7.50) $215.00
bob lennon            12.0 ($9.00)    0.0 ($13.50) $108.00