1

So, I have to do a homework problem that entails the following:

During the tax season, every Friday, J&J accounting firm privides assistance to people who prepare their own tax returns. Their charges are as follows.

a. If a person has low income (<=25,000) and the consulting time is less than or equal to 30 minutes, there are no charges; otherwise, the service charges are 40% of the regular hourly rate for the time over 30 minutes.

b. For others, if the consulting time is less than or equal to 20 minutes, there are no service charges; otherwise, service charges are 70% of the regular hourly rate for the time over 20 minutes.

(For example, suppose that a person has low income and spent 1 hour and 15 minutes, and the hourly rate is $70.00. Then the billing amount is 70.00 x 0.40 x (45 / 60) = $21.00.)

Write a program that prompts the user to enter the hourly rate, the total consulting time, and whether the person has low income. The program should output the billing amount. The program must contain a function that takes as input the hourly rate, the total consulting time, and a value indicating whether the person has low income. The function should return the billing amount. The program may prompt the user to enter the consulting time in minutes.

Here is my code:

#include <iostream>
#include <iomanip>
using namespace std;

const int HOUR = 60;
int minutes = 0;
double intake(payment);

void intake()
{
char income, y('y'), n('n');


cout << "Is the income rate over 25,000? Y - Yes | N - No): ";
cin >> income;
switch(income)
{
case 'n':
case 'N':       low_procedure()
    break;
case 'y':
case 'y':       high_procedure()
    break;
default: cout << "Invalid entry. You must indicate low or high income.\n"
  break;
}   

}

int main()
{
intake();

cout<<"You owe: \n";
cout<< payment <<endl;


}

double low_procedure()
{
const double LOW_DISCOUNT = 0.40;
const int LOW_TIME = 30;


consult = getConsultTime()
rate = getRate()

if consult > LOW_TIME
{
minutes = consult - LOW_TIME
result = rate * LOW_DISCOUNT
payment = calcPay
}

else
  cout <<"No additional fees. \n";

return payment;

}

double high_procedure()
{
const double HIGH_DISCOUNT = 0.70;
const int HIGH_TIME = 20;

consult = getConsultTime()
rate = getRate()

if consult > HIGH_TIME
{

minutes = consult - HIGH_TIME
result = rate * HIGH_DISCOUNT


}
else 
  cout<<"No additional fees.";
}

int getConsultTime()
{
int consult = 0;
cout << "How long was the consult for in minutes? \n";
    cin >> consult;

return consult;   

}

double getRate()
{
double rate = 0.00;
cout << "What was the hourly rate? \n";
  cin >> rate;

return rate;   

}

double calcPay
{
double payment = 0.00;

payment = result * (minutes/HOUR);

return payment;

}

I've been having a lot of trouble here since I realized that I need to declare variables in code. I have a feeling I'm making this more complex than it needs to be, but the switch statement is important. I'm trying to sieve through bad, unimportant data.

  • Interestingly C++ was once called "C with classes", but this question is "C++ without class" so is it a C question? – user3528438 Mar 05 '16 at 20:37
  • We simply haven't discussed classes yet, and the instructor is basically asking us we if know how to use a function properly. – user6010711 Mar 05 '16 at 20:39
  • I'm well aware it won't compile, at least for now – user6010711 Mar 05 '16 at 20:39
  • You should avoid global variables, you read can some more about it [here](http://stackoverflow.com/questions/484635/are-global-variables-bad) – ppsz Mar 05 '16 at 20:45

2 Answers2

0

You should declare them in the smallest scope that makes sense. Since you are apparently using them in multiple functions, the file scope (which is generally thought of as 'globally', not 'locally') seems appropriate.

As an alternative, you could make a class that has the variables and functions as members, but under the circumstances it seems overkill...

H. Guijt
  • 3,325
  • 11
  • 16
0

One way to do this is to group data into a struct, make a instance of that struct for each person, and make all functions accept a pointer or a reference of that struct and access its fields.

(When you learn classes then you can forget about this answer.)

There are a lot of reasons to avoid global variables (like namespace pollution).

Even if you limit them to file scope or namespace scope, there are still a lot of reasons to avoid variables with static storage duration (like thread safety, initialization order).

The rule of thumb is always "to use the smallest scope possible".

#include <iostream>
using namespace std;

struct AccountData {
    int consult;
    int minutes;
    double rate;
    double result;
    double payment;
};
AccountData makeAccount(int consult, int minutes, double rate, double result, double payment)
{
    return AccountData({consult, minutes, rate, result, payment});
}
void high_procedure(AccountData *thisAccount)
{
    cout << "do something with the account, like printing out \"payment \""
         << thisAccount->payment
         << "\n";
}
void low_procedure(AccountData *thisAccount)
{
    thisAccount->payment+=1.0;
    cout << "do something with the account, like adding 1 to \"payment \""
         << "\n";
}
int main() {
    AccountData account1 = makeAccount(1,2,3,4,5);
    high_procedure(&account1);
    low_procedure(&account1);
    high_procedure(&account1);

    AccountData account2 = makeAccount(10,20,30,40,50);
    high_procedure(&account2);
    low_procedure(&account2);
    high_procedure(&account2);

    return 0;
}

Demo: https://ideone.com/sWVoHF

user3528438
  • 2,737
  • 2
  • 23
  • 42