So the variable hoursWorked is not initialized. But how am I supposed to initialize it if I want it to equal whatever the user stores in it? For example I want hoursWorked to be whatever any person outputs in it in cin. Here is my code:
#include <iostream>
using namespace std;
int main ()
{
//Declare Variables
double hoursWorked;
double payRate;
double incomeBeforeTax;
payRate = 15;
incomeBeforeTax = payRate * hoursWorked;
cout << "Enter hours worked: ";
cin >> hoursWorked;
cout << endl;
cout << incomeBeforeTax << endl;
return 0;
}
The calculation of
incomeBeforeTaxwhich referenceshoursWorkedneeds to occur after you initialize it by reading fromcin. Move that line aftercin >> hoursWorked;and it will work:C++, like most procedural languages evaluates your code in the order in which it is written. That is,
incomeBeforeTax = payRate * hoursWorked;assigns a value toincomeBeforeTaxbased on the current values ofpayRateandhoursWorked. These must be defined and initialized before the assignment is performed. That is whatcin >> hoursWorkeddoes.On a side note,
doublevariables are best initialized withdoubleliterals so add.0to the value.