0

In this code I ask the user for his name, hours worked and rate per hour, then multiply both to get his salary. I am doing this via a pointer function but for some reason when the program goes to the calculate_salary function, it ends with an error. What is wrong and why is this happening?

#include <iostream>
#include <string>

using namespace std;

class Employee
{
    public:
        string name;
        double salary;
        double hours;
        double cash_per_hour;

        int age;
        Employee(){}
        double* salary_calculator(double *h, double *c_p_h)
        {
            double *p;
            *p = (*h) * (*c_p_h);
            cout << "here" << endl;

            return(p);
        }

        void display()
        {
            cout << endl << endl << "*********************" << endl
                 << "The salary is " << salary << endl
                 << "*********************" << endl;
        }

        void get_salary()
        {
            double *s;

            s = salary_calculator(&hours, &cash_per_hour);

            salary = *s;
        }

        void get_details()
        {
            cout << "********************************" << endl;
            cout << "WELCOME TO THE SALARY CALCULATOR" << endl;
            cout << "Please enter your name " << endl;
            cin >> name;
            cout << "Please enter the number of hours worked" << endl;
            cin >> hours;
            cout << "Please enter the rate per hour" << endl;
            cin >> cash_per_hour;
            cout << "***************END****************" << endl;
        }
};

int main()
{
    Employee one;
    one.get_details();
    one.get_salary();
    one.display();

    return 0;   
}
7
  • 4
    I hope you would try indent your code. Commented Jan 3, 2015 at 11:26
  • 1
    What error are you getting? Commented Jan 3, 2015 at 11:26
  • the program stops working after the get_details function Commented Jan 3, 2015 at 11:28
  • Why are you using pointers when you can use references? Commented Jan 3, 2015 at 11:28
  • 2
    The function salry_calculator returns a pointer to a double. However, the variable p goes out of scope. Furthermore, you use unnessary pointers such as in the function get_salary. Commented Jan 3, 2015 at 11:33

2 Answers 2

2

The pointer p in salary_calculator is not associated with any allocated space. Dereferencing it therefore results in undefined behavior.

Instead of using pointers here, just change the return type to double and adjust your program according to that.

Sign up to request clarification or add additional context in comments.

3 Comments

I used the same method for another program and it worked. It is possible to return a pointer through this method but its only this time that im getting an error
The pointer value could point to a valid location within your program but odds are it doesn't. In your other program you had luck.
@madridista "Appearing to work" is a possible symptom of undefined behaviour
0

As already stated in my comment: the function returns a pointer to the variable p. However p goes out of scope (i.e., only exist in the function itself). Therefore the pointer which is returned points to something and we do not know to what, also called a wild pointer.

To avoid this you should just return by value since this is a primitive type. When you really want to return a pointer you have to allocate memory. For an object, you can allocate memory with the function new. For example: double *p = new double;.

Keep in mind that you have to free this allocated memory when you don't need it anymore, otherwise you will have a memory leak. Read more about this here: http://www.tutorialspoint.com/cplusplus/cpp_dynamic_memory.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.