-1

I've been working on problems out of D.S. Malik C++ Programming book and I'm not looking for an answer but an explanation as to why in my charges function does billingAmount return 0 for incomeLow (Line 135). If I answer 70 for hourly rate, 15000 for customers income, and 75 for consulting time I should get $21.00 (70 * 40 * (45 / 60)) = $21.00 45 comes from subtracting 30 from 75 minutes since anything less than 30 minutes charges are free (0).

#include <iostream>
#include <iomanip>

using namespace std;

// Declared function identifiers
bool check_number(string str);

double hourly_rate();

double customer_income();

int consult_time();

double charges(double revenue, double rate, int time, bool incomeLow);

string tempRate;
string tempIncome;
string tempConsultTime;

int main()
{
    int income = 0;
    int consultTime = 0;
    double hourlyRate = 0;
    bool lowIncome = false;
    
    hourlyRate = hourly_rate();

    while (income <= 0)
    {
        income = customer_income();
    }
    
    if (income <= 25000) {
        lowIncome = true;
    }
    
    consultTime = consult_time();

    cout << fixed << showpoint << setprecision(2);
    
    cout << "Billing Amount: "
         << charges(income, hourlyRate, consultTime, lowIncome)
         << "Low Income: " << lowIncome;
    
    return 0;
}

bool check_number(string str) {
    for (int i = 0; i < str.length(); i++) {
        if (!isdigit(str[i])){
            if (str[i] == '-'){
                return false;
            }
            if (str[i] == '.'){
                return true;
            }
            return false;
            }
        }
    return true;
}

double hourly_rate()
{
    cout << "Enter the hourly rate or 'n' to exit: ";
    cin >> tempRate;

    while(!check_number(tempRate))
    {
        if (tempRate[0] == 'n')
        {
            exit(0);
        }
        
        cout << "Error: Enter a positive hourly rate or 'n' to exit: ";
        cin >> tempRate;
    }
    
    return stod(tempRate);
}

double customer_income()
{
    cout << "Enter customers income or 'n' to exit: ";
    cin >> tempIncome;

    while(!check_number(tempIncome) || tempIncome == "0")
    {
        if (tempIncome[0] == 'n')
        {
            exit(0);
        }
        
        cout << "Error: Enter a positive integer or 'n' to exit: ";
        cin >> tempIncome;
    }
    
    return stod(tempIncome);
}

int consult_time()
{
    cout << "Enter the amount of consulting time (in minutes): ";
    cin >> tempConsultTime;

    while(!check_number(tempConsultTime))
    {
        if (tempConsultTime[0] == 'n')
        {
            exit(0);
        }
        
        cout << "Error: Enter a positive consult time (in minutes) or 'n' to exit: ";
        cin >> tempConsultTime;
    }
    
    return stoi(tempConsultTime);
}

double charges(double revenue, double rate, int time, bool incomeLow){
    double billingAmount;
    int T;
    
    if (incomeLow) {
        if (revenue <= 25000 && time <= 30)
        {
            return 0;
            
        } else if (revenue <= 25000 && time > 30) {
            
            T = time - 30;
            
            billingAmount = rate * 40 * (T / 60);
        }
    }
        
    if (!incomeLow && time <= 20) {
    
        return 0;
        
    } else {
        
        T = time - 30;
        
        billingAmount = rate * 70 * (T / 60);
    }
        
    return billingAmount;
}

1 Answers1

1

You obviously think that (45 / 60) is 0.75. In ordinary math it would be, but in C++ this is integer division and the result is always an integer (truncated towards zero). In effect 0.75 is rounded down to 0.

Just change (T / 60) to (T / 60.0) and you are no longer doing integer division and the result will be 0.75.

john
  • 85,011
  • 4
  • 57
  • 81