Help with formula in final step!

Hi. I'm looking for some help figuring out the final step in my code. Here are the instructions:

---------------
The phone company uses the following rate structure for calls from San Francisco to Seattle, Washington:

Any call started at or after 6:00 pm (1800) but before 8:00 am (800) is discounted 50%.

All calls are subject to a 4% federal tax.

The regular rate is $0.35 per minute.

Any call longer than 60 minutes receives a 16% discount on its cost (after any other discount is subtracted but before tax is added).

Write a program that reads from the user the start time for a call based on a 24-hour clock and the length of the call in minutes. The gross cost (before any discount or tax) should be printed, and then the net cost (after discounts and taxes). Turn in 4 outputs for this exercise, showing that your program works in each of the following 4 cases. Use the input examples shown here, don't make up your own.
-------------

I got almost everything working great with this, and my gross cost is calculating fine. However, I am having a problem with the net cost. I cannot figure out how to get the formula right so that it calculates correctly. Any ideas on how I can fix it? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// This program calculates the cost of long-distance calls.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{   
    double pricePerMinute = 0.35; //Regular price per minute with no discounts
    double callCost;
    double grossCost;
    double netCost;
    
    int numberOfMinutes;
    int startTime;
    
    //Get user information
    cout << "Enter start time: ";
    cin >> startTime;
    cout << "Enter length of call in minutes: ";
    cin >> numberOfMinutes;
    
    //Rate structures for calls.
    if (startTime >= 801 && startTime <= 1759)
       callCost = pricePerMinute;
    else if (startTime >= 1800 && startTime <= 800)
       callCost = (50.0 / pricePerMinute);
    else if (numberOfMinutes >= 60)
       callCost = (16.0 / pricePerMinute);
       
    //Calculations.
    grossCost = numberOfMinutes * pricePerMinute;
    
    netCost = callCost + (callCost * 0.04);
    
    //Display answers.
    cout << fixed << showpoint << setprecision(2);
    cout << "Gross cost: $ " << grossCost << endl;
    
    cout << fixed << showpoint << setprecision(2);
    cout << "Net Cost: " << netCost << endl;

        return 0;
}      
Hi,
Where did you get numbers like 600 & 1800. I assume a day is 24-hour long, the maximum number associated with it is 2400?
You need to store the discount in a variable.
NetCost = grossCost - discount + tax
Use functions
closed account (48T7M4Gy)
Needs checking but try this idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// This program calculates the cost of long-distance calls.
#include <iostream>
#include <iomanip>

int main()
{
    const double pricePerMinute = 0.35; //Regular price per minute with no discounts
    double discountRate = 0.0;
    const double federalTaxRate = 0.04;
    double timeDiscountRate = 0.0;
    
    double grossCost;
    double netCost;
    
    int numberOfMinutes;
    int startTime;
    
    //Get user information
    std::cout << "Enter start time: ";
    std::cin >> startTime;
    std::cout << "Enter length of call in minutes: ";
    std::cin >> numberOfMinutes;
    
    //Rate structures for calls.
    
    if (startTime >= 800 && startTime < 1800)
        discountRate = 0.0;
    else
        discountRate = 0.5;
    
    if (numberOfMinutes > 60)
        timeDiscountRate = 0.16;
    else
        timeDiscountRate = 0;
    
    //Calculations.
    grossCost = numberOfMinutes * pricePerMinute;
    netCost = grossCost * (1 - discountRate ) * (1 - timeDiscountRate) * (1 + federalTaxRate);
    
    // 4 cases - 2 for duration, 2 for time zone
    
    //Display answers.
    std::cout << std::fixed << std::showpoint << std::setprecision(2);
    std::cout << "Gross cost: $" << grossCost << std::endl;
    
    std::cout << std::fixed << std::showpoint << std::setprecision(2);
    std::cout << "Net Cost: $" << netCost << std::endl;
    
    return 0;
}
Last edited on
Hi everyone! Thanks so much for the responses. I tried all of your suggestions, but I am still not getting the correct net cost. Any other suggestions?
Does your assignment contain output samples?

Enter start time: 1000
Enter length of call in minutes: 30


Can you calculate the expected net cost based on these?
Hi, yes here is an output sample given:

Enter start time: 2322
Enter length of call in minutes: 67
gross cost: $23.45
net cost: $10.24

I am able to get the $23.45 with what I have, just not the net cost. I cannot figure out the correct way to write the formula.
This is what I have now:

// This program calculates the cost of long-distance calls.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
const double pricePerMinute = 0.35; //Regular price per minute with no discounts
const double federalTaxRate = 4.0;

double grossCost;
double netCost;
double discountRate;

int numberOfMinutes;
int startTime;

//Get user information
std::cout << "Enter start time: ";
std::cin >> startTime;
std::cout << "Enter length of call in minutes: ";
std::cin >> numberOfMinutes;

if (startTime >= 801 && startTime <= 1759)
discountRate = numberOfMinutes * pricePerMinute;
else if (startTime >= 1800 && startTime <= 800)
discountRate = 50.0 / (numberOfMinutes * pricePerMinute);
if (numberOfMinutes <= 60)
discountRate = numberOfMinutes * pricePerMinute;
else if (numberOfMinutes >= 60)
discountRate = 16.0 / (numberOfMinutes * pricePerMinute);

grossCost = numberOfMinutes * pricePerMinute;
netCost = federalTaxRate / discountRate;

std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << "Gross cost: $" << grossCost << std::endl;

std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << "Net Cost: $" << netCost << std::endl;

return 0;
}


I am a little closer to the correct net cost answer with this, but it's still not calculating correctly. Here is my updated output:

Enter start time: 2322
Enter length of call in minutes: 67
Gross cost: $23.45
Net Cost: $5.86

Here is what the correct output is supposed to be:

Enter start time: 2322
Enter length of call in minutes: 67
gross cost: $23.45
net cost: $10.24








Note: I calculated this by hand

Enter start time: 2322
Enter length of call in minutes: 67

1. Gross cost
0.35 * 67 = 23.45 (Ok)

2. Net cost
> Any call started at before 8:00 am (800) but after 6:00 pm (1800) is discounted 50%.
==> 0.35 * 67 * 0.50

> All calls are subject to a 4% federal tax.
==> 0.35 * 67 * 0.50 * 1.04

> Any call longer than 60 minutes receives a 16% discount on its cost (after any other discount is subtracted but before tax is added).
==> (0.35 - 0.35 * 0.16) * 67 * 0.50 * 1.04

Final net cost :
(0.35 - (0.35 * 0.16)) * 67 * 0.50 * 1.04 = 10.24296
This should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// This program calculates the cost of long-distance calls.
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
  double pricePerMinute = 0.35; //Regular price per minute with no discounts
  double discount = 0, grossCost = 0, netCost = 0;

  int numberOfMinutes;
  int startTime;

  //Get user information
  cout << "Enter start time: ";
  cin >> startTime;
  cout << "Enter length of call in minutes: ";
  cin >> numberOfMinutes;

  grossCost = numberOfMinutes * pricePerMinute;
  
  if (startTime >= 800 && startTime <= 1800)
  {
    netCost = grossCost; // no discount on time
  }
  else
    netCost = grossCost * 0.5; // 50% discount

  if (numberOfMinutes >= 60)
    netCost -= netCost * 0.16; // another 16% discount

  double tax = netCost * 4 / 100; // 4% tax
  
  netCost = netCost + tax;

  //Display answers.
  cout << fixed << showpoint << setprecision (2);
  cout << "Gross cost: $ " << grossCost << endl;

  cout << fixed << showpoint << setprecision (2);
  cout << "Net Cost: " << netCost << endl;
  system ("pause");
  return 0;
}


output:

Enter start time: 2322
Enter length of call in minutes: 67
Gross cost: $ 23.45
Net Cost: 10.24
My solution :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// This program calculates the cost of long-distance calls.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
const double pricePerMinuteOriginal = 0.35; 
double pricePerMinute = pricePerMinuteOriginal;

// Regular price per minute with no discounts

const double federalTaxRate = 1.04;
double grossCost;
double netCost;

int numberOfMinutes;
int startTime;
//Get user information
std::cout << "Enter start time: ";
std::cin >> startTime;
std::cout << "Enter length of call in minutes: ";
std::cin >> numberOfMinutes;

if (startTime < 800 || startTime > 1800)
pricePerMinute *= 0.5;

if (numberOfMinutes > 60)
pricePerMinute -= pricePerMinute * 0.16;

grossCost = numberOfMinutes * pricePerMinuteOriginal;

netCost = pricePerMinute * numberOfMinutes * federalTaxRate;

std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << "Gross cost: $" << grossCost << std::endl;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << "Net Cost: $" << netCost << std::endl;
return 0;
}
Last edited on
Does that help you? :)
Thank you guys SO much! Those last two worked right when I was about to give up ;)
Glad to hear :)
closed account (48T7M4Gy)
Enter start time: 2322
Enter length of call in minutes: 67
Gross cost: $23.45
Net Cost: $10.24
closed account (48T7M4Gy)
It might seem too fine a point but in actual fact it is a very important where software is subject to audit in companies and where tax rates and discount rates are used along with similar business rules it is generally accepted to separate the actual rate from the way it is applied.

For instance the modification factor for tax is (1 - taxRate) and the taxRate is .04 (4% as stated) and not 1.04 (104%) which is misleading. Just sayin' ...
Topic archived. No new replies allowed.