简体   繁体   English

C++ 无法从方法中计算出工资

[英]C++ unable cout salary from method

Entry point is int main() so I try summon pwr.GetSalary to cout outside string "Salary" and double value, however program does not print out anything.入口点是 int main() 所以我尝试调用 pwr.GetSalary 来计算外部字符串“Salary”和双值,但是程序没有打印出任何东西。 So it is base class.所以它是基类。

class Employee
{
    public:
        std::string FirstName;
        std::string LastName;
        std::string Patronymic;
        double Salary;
        Employee() {};
        explicit Employee(std::string FirstName, std::string LastName,
            std::string Patronymic, double Salary)
            : FirstName(FirstName), LastName(LastName),
            Patronymic(Patronymic), Salary(Salary) {}
        bool operator==(Employee other) const
        {
            if (this->FirstName == other.FirstName && 
                this->LastName == other.LastName && 
                this->Patronymic == other.Patronymic) 
                return true;
            else 
                return false;
        }
};

An daughter class that inherits base class... Here is wonderful method that shall count a salary and print it out...一个继承基类的子类......这是一个很好的方法,可以计算工资并打印出来......

class Papersworker : public Employee
{
    private:
        std::string FirstName;
        std::string LastName;
        std::string Patronymic;
        double Salary;
    public:
        Papersworker() {};
        using Employee::Employee;
        const std::string Job = "Papersworker";
        std::map<std::string, double> Coefficient = 
        {
            {"Director", 4.2},
            {"Papersworker", 1.2},
            {"Guardian", 1.5},
            {"Programmer", 2.5}
        };
        void ChangeCoefficient(std::string Job, double NewCoefficient)
        {
            Coefficient[Job] = NewCoefficient;
        }
        void ChangeNameSalary(std::string FirstName, std::string LastName, std::string Patronymic, double Salary)
        {
            this->FirstName = FirstName;
            this->LastName = LastName;
            this->Patronymic = Patronymic;
            this->Salary = Salary;
        }
        void PrintPapersworker()
        {
            std::cout << "First name\t" << "Lastname\t" << "Patronymic\t" << "Salary\n" << this->FirstName << "\t\t" << this->LastName << "\t" << this->Patronymic << "\t" << this->Salary << "\n" << std::flush;
            for (const auto& i : this->Coefficient)
            {
                std::cout << i.first << " = " << i.second << ";\t" << std::flush;
            }
            std::cout << "\n------------------------------------------------------------\n" << std::flush;
        }
        double GetSalary(double Salary, std::string Job)
        {
            return Salary * this->Coefficient[Job];
        }
};

Wonderful int main()'s part.精彩的 int main() 部分。

int main()
{
   Papersworker pwr;
   double sr = 0.0;
   std::cout << "\nEnter director's salary\t" << std::flush; std::cin >> sr;
   std::cout << "\nSalary\t" << pwr.GetSalary(sr, "Director");
   return 0;
}

If you see a some bad and need optimization don't mind to reply.如果您看到一些不好的内容并需要优化,请不要介意回复。 ._. ._. I do not understand what is going on there in matter of classes building tricks.我不明白在课堂构建技巧方面发生了什么。 https://pastebin.com/p7HXaX80 PS My homework forces to use private FirstName,LastName,Patronymic,salary... PSS However, I use Visual Studio 2022 Preview with newest C++ nowadays. https://pastebin.com/p7HXaX80 PS 我的家庭作业强制使用私人名字、姓氏、父系成员、薪水... PSS 但是,我现在使用带有最新 C++ 的 Visual Studio 2022 预览版。 https://imgur.com/a/N8cDK3n https://imgur.com/a/N8cDK3n

program does not print out anything程序不打印任何东西

Your program(pastebin link you gave) compiles successfully and prints continuously if you change _getch() to getch() as can be seen here .如果你改变你的计划(你给引擎收录链接)编译成功,并连续打印_getch()getch()可以看出这里 But for some reason it goes on forever.但出于某种原因,它会永远持续下去。 Since the link that you gave have around 500 lines of code i didn't take a look as to why the condition is not breaking(or if the program has any undefined behavior).由于您提供的链接有大约 500 行代码,我没有考虑为什么条件没有中断(或者程序是否有任何未定义的行为)。 Maybe you can narrow down the problem and edit your question again.也许您可以缩小问题的范围并再次编辑您的问题。

All of the private members of Papersworker shadow the public members from Employee Papersworker所有私有成员都PapersworkerEmployee的公共成员

class Papersworker : public Employee {
 private:
  std::string FirstName;
  std::string LastName;
  std::string Patronymic;
  double Salary;
// ...
};

Try this: Compiler Explorer (Untested 'cause I'm in a rush right now, I'll look at it when I come back)试试这个:编译器资源管理器(未测试,因为我现在很着急,等我回来看看)

Also, be careful with std::map::operator []另外,要小心std::map::operator []

Your code will not compile as sr variable is not defined.由于未定义sr变量,您的代码将无法编译。
Define double sr;定义double sr; before using it in main()'s statement std::cin >> sr;在 main() 的语句中使用它之前std::cin >> sr; and (at least) the program will compile and interact with the user.并且(至少)程序将编译并与用户交互。

int main() {
  Papersworker pwr;
  std::cout << "\nEnter director's salary\t" << std::flush;
  double sr; // <- your missed variable
  std::cin >> sr;
  std::cout << "\nSalary\t" << pwr.GetSalary(sr, "Director");
}

Program's prints with input 10:输入 10 的程序打印:

Enter director's salary 10

Salary  42

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM