输出有时会失败

时间:2018-09-23 03:10:26

标签: c

我正在尝试使用C程序查找月工资总额。我的输出有时会失败。将代码和输出放在下面:

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

void main() {
  int id;
  float hours;
  float mgsalary;
  clrscr();
  printf(
      " Press ID  : \n Press 1 if Staff: \n Press 2 if Supervisor: \n Press 3 if Manager: \n Press 4 if President: \n");
  scanf("%d", &id);
  printf(" Enter hours per week \n");
  scanf("%f", &hours);
  if (id == 1) {
    printf(" Position ID: 1 \n");
    mgsalary = (hours * 62.5) * 4.28;
    printf(" Positon/Rank: Staff \n");
    printf(" Rate per hour: 62.5 php \n");
    printf(" Monthly Gross Salary : %f php \n ", mgsalary);
  } else if (id == 2) {
    mgsalary = (hours * 125) * 4.28;
    printf(" Position ID: 2 \n");
    printf(" Position/Rank: Supervisor \n");
    printf(" Rate per hour: 125 php \n");
    printf(" Monthly Gross Salary : %f php \n", mgsalary);
  } else if (id == 3) {
    mgsalary = (hours * 187.5) * 4.28;
    printf(" Position ID: 3 \n");
    printf(" Position/Rank: Manager \n");
    printf(" Rate per hour: 187.5 php \n");
    printf(" Monthly Gross Salary : %f php \n", mgsalary);
  } else if (id == 4) {
    mgsalary = (hours * 375) * 4.28;
    printf(" Position ID: 4 \n");
    printf(" Position/Rank: President \n");
    printf(" Rate per hour: 375 php \n");
    printf(" Monthly Gross Salary: %f php \n", mgsalary);
  } else
    printf(" Invalid Input");
  getch();
}

//运行程序(输出)

Press 1 if Staff: 
Press 2 if Supervisor:
Press 3 if Manager: 
Press 4 if President:

//我通过按1选择了Staff

Enter numbers of hours work:

//我输入的工作时间为33

Position ID: 1 
Positon/Rank: Staff 
Rate per hour: 62.5 php 
Monthly Gross Salary : 8872.5000 php (sometimes it came out 10000000.000 php or null)

1 个答案:

答案 0 :(得分:-1)

以下建议的代码:

  1. 消除了来自conio.h的非便携式呼叫
  2. 正确检查scanf()错误
  3. 格式化代码以提高可读性
  4. 使用switch()语句,而不是一系列if()语句
  5. 正确声明具有有意义名称的“魔术”数字
  6. 利用enum语句为用户选择输入有意义的名称
  7. 正确地将所有文字值声明为float,而不是默认的double
  8. 干净地编译
  9. main()函数使用有效的签名
  10. 编辑命名,按评论进行更正

现在,建议的代码:

#include <stdio.h>   // perror(), getchar(), fprintf(), printf(), scanf()
//#include <conio.h>
#include <stdlib.h>  // exit(), EXIT_FAILURE

#define WEEKS_PER_MONTH           4.28f
#define STAFF_WEEKLY_RATE        62.5f
#define SUPERVISOR_WEEKLY_RATE  125.0f
#define MANAGER_WEEKLY_RATE     187.5f
#define PRESIDENT_WEEKLY_RATE   375.0f

enum 
{
    dummy,
    STAFF,
    SUPERVISOR,
    MANAGER,
    PRESIDENT
};


int main( void ) 
{
    int id;
    float hours;
    float mgsalary;
    int ch;



    /* clrscr();  Suggest using the ANSI excape sequences */
    printf( "%s\n",
          " Press ID  : \n"
          " Press 1 if Staff: \n"
          " Press 2 if Supervisor: \n"
          " Press 3 if Manager: \n"
          " Press 4 if President: \n");
    id = getchar();

    printf(" Enter hours per week \n");
    if( scanf("%f", &hours) != 1 )
    {
        fprintf( stderr, "scanf for hours failed\n" );
        exit( EXIT_FAILURE );
    }


    switch( id )
    {
      case STAFF:
        printf(" Position ID: 1 \n");
        mgsalary = (hours * STAFF_WEEKLY_RATE) * WEEKS_PER_MONTH;
        printf(" Positon/Rank: Staff \n");
        printf(" Rate per hour: 62.5 php \n");
        printf(" Monthly Gross Salary : %f php \n ", mgsalary);
        break;

      case SUPERVISOR:
        mgsalary = (hours * SUPERVISOR_WEEKLY_RATE) * WEEKS_PER_MONTH;
        printf(" Position ID: 2 \n");
        printf(" Position/Rank: Supervisor \n");
        printf(" Rate per hour: 125 php \n");
        printf(" Monthly Gross Salary : %f php \n", mgsalary);
        break;

      case MANAGER:
        mgsalary = (hours * MANAGER_WEEKLY_RATE) * WEEKS_PER_MONTH;
        printf(" Position ID: 3 \n");
        printf(" Position/Rank: Manager \n");
        printf(" Rate per hour: 187.5 php \n");
        printf(" Monthly Gross Salary : %f php \n", mgsalary);
        break;

      case PRESIDENT:
        mgsalary = (hours * PRESIDENT_WEEKLY_RATE) * WEEKS_PER_MONTH;
        printf(" Position ID: 4 \n");
        printf(" Position/Rank: President \n");
        printf(" Rate per hour: 375 php \n");
        printf(" Monthly Gross Salary: %f php \n", mgsalary);
        break;

      default:
        printf(" Invalid Menu Selection\n");
        break;
    } /* end switch */

    while( (ch = getchar()) != EOF && ch != '\n' );
    getchar();
}