簡體   English   中英

為什么我第二次運行循環時該程序崩潰?

[英]Why does this program crash when I run through the loop a 2nd time?

盡我所能解決這個難題,我想我正在失去理智! 誰能幫助我理解為什么我第二次運行循環時該程序崩潰?

我可以運行一次交互式循環,並將輸入的值寫到文件中。 但是,當我第二次嘗試通過循環時,程序會阻塞。

// 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM