dimanche 8 mars 2020

C++ If statement nested in while loop not switching to 'else'

Hi I think I'm going a little cross-eyed or I'm just losing my mind but I can't seem to figure out exactly why the below code does not work as intended.

The part that I'm referring to is the nested 'if' statement in the while loop not executing the 'else' when the first condition returns false. I cannot see anything wrong with it, please help.

PS. I know the code is not optimised/thoroughly refactored, it was just something I threw together to test an activity in my textbook.

#include <iostream>
using namespace std;
int main( )
{

float shiftStart, shiftEnd, totalDailyEarnings, hoursCalcd;

hoursCalcd = 0;

totalDailyEarnings = 0;

float eveningRate = 44;
float dayRate = 30.5;

cout << "Enter shift start time:" << endl;
cin >> shiftStart;


hoursCalcd = shiftStart;

cout << "Enter shift end time:" << endl;
cin >> shiftEnd;

while (hoursCalcd < shiftEnd)
{
    cout << "Hours calcd: " << hoursCalcd << endl;
    cout << "dayRateApplied " << (hoursCalcd > 0 && hoursCalcd < 6) << endl;

    if (hoursCalcd > 0)
    {
        if(hoursCalcd < 6){
            totalDailyEarnings += 30.5;
        }else{
            totalDailyEarnings += 44;
        }
    }


    cout << "Total incremented by: R" << dayRate << " , daily earnings currently at: R"<< totalDailyEarnings << endl;
    hoursCalcd++;
}

cout << endl << "TOTAL DAILY EARNING: R" << totalDailyEarnings << endl;

return 0;
}

Aucun commentaire:

Enregistrer un commentaire