Home > Net >  C unable cout salary from method
C unable cout salary from method

Time:11-16

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. 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()
{
   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 P. S. My homework forces to use private FirstName,LastName,Patronymic,salary... P. S. S. However, I use Visual Studio 2022 Preview with newest C nowadays. https://imgur.com/a/N8cDK3n

CodePudding user response:

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. 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). Maybe you can narrow down the problem and edit your question again.

CodePudding user response:

Your code will not compile as sr variable is not defined.
Define double sr; before using it in main()'s statement 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:

Enter director's salary 10

Salary  42

CodePudding user response:

All of the private members of Papersworker shadow the public members from Employee

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 []

  • Related