Member Avatar for johny112

Im getting 2 of these warning message upon compiling the programme below. Cant figure out why. can someone help please?

warning C4700: local variable 'rate_of_pay' used without having been initialized
warning C4700: local variable 'hours' used without having been initialized

Question
Develop a C++ program that uses a while structure to determine the gross pay for each of several employees. The company pays "flat-rate" for the first 40 hours worked by each employee and pays “1.5 times flat rate” for all hours worked in excess of 40 hours. Your program should prompt the user for information on each employee (as shown below) and should determine and display the employee’s gross pay. A terminator should end the input prompt loop. You only need to enter enough employees and their details to demonstrate the program operation.

#include <iostream>
using namespace std;
int main ()
{
    int hours, rate_of_pay, salary_with_overtime, salary_without_overtime;


    salary_without_overtime = rate_of_pay * hours;
    salary_with_overtime = (40 * rate_of_pay) + ((hours - 40)* 1.5 * rate_of_pay);

    do {
        cout << "Enter total number of hours worked : ";
        cin >>hours;

        cout << "Enter hourly rate of worker : ";
        cin >> rate_of_pay;

            if (hours <= 40) {
            cout <<"Salary is  : "<< salary_without_overtime<<endl;
        }
    else {
                cout <<"Salary is : " <<salary_with_overtime<<endl;
        }

    } while (hours >0); 

    return 0;
}

>Cant figure out why.
Um, because you're using those two variables before giving them a value.

>int hours, rate_of_pay, salary_with_overtime, salary_without_overtime;
>salary_without_overtime = rate_of_pay * hours;
>salary_with_overtime = (40 * rate_of_pay) + ((hours - 40)* 1.5 * rate_of_pay);
Look at the first line, then look at the next two lines and tell me what values hours and rate_of_pay have.

Member Avatar for johny112

thanks for reply, i have got rid of warning messages but i have new problem now. when i execute the program, the answer after first set of values is right, but the answers for all the values followed by are wrong.

Replies appreciated


This is the new modified programme.
-------------------------------------------------------

#include <iostream>
using namespace std;

float main ()
{
	float hours, rate_of_pay, salary_with_overtime, salary_without_overtime;
	
	cout << "Enter total number of hours worked : ";
   	cin >>hours;

    cout << "Enter hourly rate of worker : ";
    cin >> rate_of_pay;
	
	salary_without_overtime = rate_of_pay * hours;
	salary_with_overtime = (40 * rate_of_pay) + ((hours - 40)* 1.5 * rate_of_pay);
	
	do {
	   if (hours <= 40) {
			cout <<"Salary is  : "<< salary_without_overtime<<endl;
		}
	   else {
			cout <<"Salary is : " <<salary_with_overtime<<endl;
		 }

      cout << "Enter total number of hours worked : ";
   	  cin >>hours;
 
      cout << "Enter hourly rate of worker : ";
      cin >> rate_of_pay;

	} while (hours > 0);	

	return 0;
}

----------------------------------------------------

you need to move lines 14 and 15 inside the do loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.