【问题标题】:Problems with Employee Summary at the end not showing最后没有显示员工摘要的问题
【发布时间】:2013-12-25 23:33:36
【问题描述】:

我一切正常,但当我在上交之前去检查它时,出了点问题,我无法让它在最后运行员工摘要。有人可以指导我了解我在这里缺少的部分吗?我还不断收到 3 个警告 C$@$$ "=" 从 double 转换为 float 可能丢失数据?第 99、101 和 109 行?:

    // ConsoleApplication29.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <iomanip>

    using namespace std;

    //
    //CLASS DECLARATION SECTION
    //
    class EmployeeClass
     {
    public:
    void ImplementCalculations(string EmployeeName, int hours, double wage);
    void DisplayEmployInformation(void);
    void Addsomethingup (EmployeeClass Emp1, EmployeeClass Emp2, EmployeeClass Emp3);
    string EmployeeName ;
    int hours ;
    float wage ;
    float basepay ;
    int overtime_hours ;
    float overtime_pay ;
    float overtime_extra ;
    float iTotal_salaries ;
    float iIndividualSalary ;
    int iTotal_hours ;
    int iTotal_OvertimeHours ;
    };

    int main()
    {   system("cls"); 

    cout << "\nWelcome to the Employee Pay Center\n\n" ;

    EmployeeClass Emp1;
    EmployeeClass Emp2;
    EmployeeClass Emp3;


    cout << "Enter the first employee's name      = ";
    cin >> Emp1.EmployeeName;
    cout << "\nEnter the hours worked               = ";
    cin >> Emp1.hours;
    cout << "\nEnter their hourly wage         = ";
    cin >> Emp1.wage;
    cout << endl;

    cout << "Enter the second employee's name      = ";
    cin >> Emp2.EmployeeName;
    cout << "\nEnter the hours worked               = ";
    cin >> Emp2.hours;
    cout << "\nEnter their hourly wage         = ";
    cin >> Emp2.wage;
    cout << endl;

    cout << "Enter the third employee's name      = ";
    cin >> Emp3.EmployeeName;
    cout << "\nEnter the hours worked               = ";
    cin >> Emp3.hours;
    cout << "\nEnter their hourly wage         = ";
    cin >> Emp3.wage;


    cout << endl;
    Emp1.ImplementCalculations(Emp1.EmployeeName, Emp1.hours, Emp1.wage);
    Emp2.ImplementCalculations(Emp2.EmployeeName, Emp2.hours, Emp2.wage);
    Emp3.ImplementCalculations(Emp3.EmployeeName, Emp3.hours, Emp3.wage);


    system ("pause");
    return 0;


    //This section you will send all three objects to a function that will add up the the         following information:
    //- Total Employee Salaries 
    //- Total Employee Hours
    //- Total Overtime Hours

    //The format for this function is the following:
    //- Define a new object.
    //- Implement function call [objectname.functionname(object name 1, object name 2, object name 3)]


    } //End of Main Function


    void EmployeeClass::ImplementCalculations (string EmployeeName, int hours, double wage)    {
    //Initialize overtime variables
    overtime_hours=0;
    overtime_pay=0;
    overtime_extra=0;

    if (hours > 40) 
    {       

    basepay = 40 * wage;
    overtime_hours = hours - 40;
    overtime_pay = wage * 1.5;
    overtime_extra = overtime_hours * overtime_pay;
    iIndividualSalary = overtime_extra + basepay;

    }   // if (hours > 40)
    else
    {   

    basepay = hours * wage;
    iIndividualSalary = basepay;

    }   
    DisplayEmployInformation ();

    } //End of Primary Function

    void EmployeeClass::DisplayEmployInformation () 
    {
    // This function displays all the employee output information.

    cout << "\n\n";
    cout << "Employee Name ............. = " << EmployeeName << endl;
    cout << "Base Pay .................. = " << basepay << endl;
    cout << "Hours in Overtime ......... = " << overtime_hours << endl;
    cout << "Overtime Pay Amount......... = " << overtime_extra << endl;
    cout << "Total Pay ................. = " << iIndividualSalary << endl;

    } // END OF Display Employee Information

    void EmployeeClass::Addsomethingup (EmployeeClass Emp1, EmployeeClass Emp2,     EmployeeClass Emp3)
    {

   iTotal_salaries = 0;
   iTotal_hours = 0;
   iTotal_OvertimeHours = 0;
      for (int i = 0; i < 3; ++i )
     {
        cout << "\n\n";
        cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
        cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl;
        cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
        cout << "%%%% Total Employee Salaries ..... =" << iTotal_salaries << endl;
        cout << "%%%% Total Employee Hours ........ =" << iTotal_hours << endl;
        cout << "%%%% Total Overtime Hours......... =" << iTotal_OvertimeHours << endl;
        cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
        cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
     }
        system("PAUSE");
        return;
     } // End of function

【问题讨论】:

  • 抱歉,警告是 C4244。
  • 您真的应该将代码缩减为合理的测试用例。如果没有行号,很难确定哪一行是 99...
  • 我也会删除不适用的标签。我知道 c# 已被删除,但 c 和 java 也应该删除。
  • 对不起,我以为它编号了。就是代码中的 If/Else 语句。
  • PS:你的缩进是假的,这可能是让你失望的原因。考虑找一个代码编辑器或一个为你维护缩进的 IDE,这样会更容易发现这样的错误。

标签: java c++ architecture


【解决方案1】:

(1) 我在 MSVC 2010 下编译/运行了它,但没有收到类型警告。但是,警告问题可能是因为您混合了类型——工资、基本工资和加班工资都是浮点类型,并且您正在使用 int 类型的变量进行计算(文字 40 和小时) a - 使用 40f 而不是 40(使用浮点常量而不是 int)和 b - 在这些语句中使用 static_cast(hours) 而不是仅 hours c - 考虑使用 double 类型而不是 float - 无论如何,使用 double 类型可以获得比 float 类型更好的精度。

正如 Mornfall 所说,您永远不会调用 AddSomethingUp 成员函数。

对该函数的唯一引用是类描述中的原型头和实际代码本身——你不能在任何地方调用它。

你可以在奶牛回家之前构建函数式代码,但除非你真的在某个地方告诉程序运行它,否则它不会被执行。您需要在处理完员工数据之后以及从 main 执行“return”语句之前调用它。

这是使用某些版本的 Visual Studio 编写的,以下几行是致命的赠品:

// ConsoleApplication29.cpp : Defines the entry point for the console application.`
//
#include "stdafx.h"

您是否尝试在调试器下运行它以查看调用了哪些代码?

【讨论】:

  • 对不起你们。我不得不离开。我一直在处理这件事,并且在医院里有一个亲人。我试图弄清楚在哪里声明它,但我遇到了困难。我调试了它,但现在唯一出现的是它没有在类之外声明。我不明白?
  • 您好,我现在唯一遇到的问题是它说“期望在“{”上声明...就在“// END OF Display Employee Information”之后
【解决方案2】:

您永远不会致电Addsomethingup,因此它永远不会打印摘要。将双精度数转换为浮点数当然会失去精度。始终对所有变量使用浮点数或双精度数(即,除非您有充分的理由不这样做,否则不要混合这些变量,而您可能在这里没有这样做)。

【讨论】:

  • 这会是我一开始在声明部分中所说的吗?
  • 不,声明不会调用任何东西。我认为你还应该买一本关于 C++ 基础的书,或者一门课程。函数调用其他函数,在这种情况下 main() 调用了一些 (ImplementCalculations) ...但对 Addsomethingup 的调用不会出现在任何地方,因此不会执行。
  • 如何开始解决为变量转换双精度或浮点的问题?这部分我真的很笨拙。大约一周前,我们花了大约 4 天的时间来解决这个问题,但似乎还不足以理解。任何有用的资源将不胜感激,因为我真的对这个主题感兴趣。:-)
  • 根本不要使用float。在任何地方使用double。或相反亦然。与double 相比,float 的位数更少,因此从double 转换为float 可能会丢失数字中的一些最低有效位。
  • 是的!将一切更改为双倍工作!我很感激。我试图找到更多关于这方面的信息,因为我的教科书是从网上下载的,我们只下载了我们需要的章节,所以我没有太多讨论这个话题的章节。我刚刚在网上找到了一个我刚买的以帮助我更多的东西。我仍然想弄清楚的唯一一件事是如何让员工摘要显示在最后。我正在尝试考虑 stackoverflow.com/users/2968458/mornfall 告诉我的内容。
猜你喜欢
  • 2013-03-08
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2012-09-25
  • 2016-02-13
  • 1970-01-01
相关资源
最近更新 更多