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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

>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.

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

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

This article has been dead for over six months. Start a new discussion instead.