0

This program calculates the service rate for tax assistance based on whether a client is low income or not and how much time they need.

The problem is that some if statements return 0 even though the operands are the correct numbers. Is this the dreaded null pointer I keep hearing about?

The problem is that some if statements return 0 even though the operands are the correct numbers. Is this the dreaded null pointer I keep hearing about?

#include <iostream>
#include <iomanip>

using namespace std;

//declare prototype for boolean lowIncome function
bool lowIncome(double income);

//declare prototype for billingAmount function
double billingAmount(double rate, int cTime, bool isLowIncome);

int main() {
    //declare global variables
    double income;
    double rate;
    bool isLowIncome;
    int cTime;
    double billAmount;

    //format to two decimal places
    cout << fixed << showpoint << setprecision(2);
    //prompt user for yearly income, place in income
    cout << "Enter yearly income: ";
    cin >> income;
    cout << endl;
    //prompt user for hourly rate, place in rate
    cout << "Enter hourly rate: ";
    cin >> rate;
    cout << endl;
    //prompt user for total consulting time in minutes
    cout << "Enter total consulting time in minutes: ";
    cin >> cTime;
    cout << endl;

    //capture low income status
    isLowIncome = lowIncome(income);

    //capture billing amount
    billAmount = billingAmount(rate, cTime, isLowIncome);

    //output billing amount to user
    cout << "The billing amount is: " << billAmount << endl;
    //cout << "Client is low income: " << isLowIncome;

    return 0;
}

//define lowIncome function
bool lowIncome(double income)
{
  if(income<=25000)
  {
    return true;
  } else 
    {
      return false;
    }
}

//define billingAmount function
double billingAmount(double rate, int cTime, bool isLowIncome)
{
  double billAmount = 1;
  int overtime;
  if (isLowIncome == true && cTime<=30)
  {
     billAmount = 1;
     cout << "bill 1 is: " << billAmount << endl;
  } else if (isLowIncome == true && cTime>30)
    {
      cTime = cTime - 30;
      billAmount = rate * .4 * (cTime/60);
      cout << "bill 2 is: " << billAmount << endl;
    } else if (isLowIncome == false && cTime<=20)
      {
        billAmount = 0;
      } else if (isLowIncome == false && cTime>20)
        {
          overtime = cTime - 20;
          billAmount = rate * .7 * (overtime/60);
          cout << "bill 3 is: " << billAmount << endl; 
        } else 
          {
            billAmount = 0;
          }
    return billAmount;
}
EricMPastore
  • 339
  • 2
  • 7
  • There aren't any pointers or references anywhere in this code, so I doubt it has anything to do with null pointers. Can you give an example of the input you gave that incorrectly returned `0` along with what you would have expected the output to be in that case? – Nathan Pierson Mar 07 '21 at 05:20
  • 1
    Actually this is pretty answerable even without seeing an input/expected pair. The issue is with things like `(cTime/60)` not doing what you expect--see [here](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division). `cTime` is an integer therefore `(cTime/60)` is an integer which means that if `0 < cTime < 60`, `(cTime/60)` is `0` not some positive fraction. – Nathan Pierson Mar 07 '21 at 05:21
  • Well f***. That is the problem. And it sounds like this is a little wonky. How do I fix it? – EricMPastore Mar 07 '21 at 05:26
  • It's not particularly wonky, just unexpected from some people. You can force it to do floating point arithmetic by making one of the operands a floating point value, which in your case is as simple as replacing `(cTime/60)` with `(cTime/60.)`, and similarly for overtime. (`60.` is a `double` instead of an `int`.) – Nathan Pierson Mar 07 '21 at 05:29
  • Nevermind. I changed cTime to a double and it fixed it. Thanks. By the way, if you want to earn some points, you can put your comment into an answer so I can mark it correct if you want. – EricMPastore Mar 07 '21 at 05:29
  • I can't find a duplicate right now but I can't imagine that there aren't several questions that all boil down to basically this exact issue, it's a common source of bugs even among non-beginners. – Nathan Pierson Mar 07 '21 at 05:30

0 Answers0