首页 > 解决方案 > C ++无法从方法中计算薪水

问题描述

入口点是 int main() 所以我尝试调用 pwr.GetSalary 来计算外部字符串“Salary”和双倍值,但是程序不打印任何内容。所以它是基类。

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;
        }
};

继承基类的子类...这是计算薪水并打印出来的绝妙方法...

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];
        }
};

精彩的 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;
}

如果您看到一些不好并且需要优化,请不要介意回复。._. 我不明白在课程建设技巧方面发生了什么。https://pastebin.com/p7HXaX80 PS 我的作业强制使用私有 FirstName、LastName、Patronymic、salary... PSS 但是,我现在使用 Visual Studio 2022 Preview 和最新的 C++。 https://imgur.com/a/N8cDK3n

标签: c++visual-c++

解决方案


程序不打印任何东西

您的程序(您提供的 pastebin 链接)编译成功并连续打印,如果您更改_getch()getch()可以看到这里。但由于某种原因,它会永远持续下去。由于您提供的链接有大约 500 行代码,我没有看一下为什么条件没有破坏(或者程序是否有任何未定义的行为)。也许您可以缩小问题范围并再次编辑您的问题。


推荐阅读