Using switch/ else if

My code is not giving the desired output. I was wondering if you guys had any input. I am currently working on another version, however if there is a way to fix this one i'd love to know.





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.

Sample output 1:


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


Enter start time: 759
Enter length of call in minutes: 10
gross cost: $3.50
net cost: $1.82
Sample output 3:


Enter start time: 1300
Enter length of call in minutes: 100
gross cost: $35.00
net cost: $30.58
Sample output 4:


Enter start time: 1300
Enter length of call in minutes: 10
gross cost: $3.50
net cost: $3.64

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include <iostream>
using namespace std;
int main()
{
    // Assign variables and values
        int start_time;
        int Length_of_call;
        const float federal_tax = .04;
        const float regular_rate = .35;
        const float with_discount = .05;
        const float discount_PerMin = .16;
        float gross_Cost;
        float PriceofStartTime;
        float PriceofLengthofCall;
        float net_Cost;

    cout << " Enter start time (0-2400):" <<endl;
    cin>> start_time;
    cout<< "Enter the length of the call (minutes):" <<endl;
    cin>> Length_of_call;

    //Discounted PriceofStartTime. ( by .05)
    if (start_time >=1800 || start_time<800)
    if (Length_of_call<60)
    { PriceofStartTime = (start_time*federal_tax);
    PriceofLengthofCall = (Length_of_call*regular_rate);
    gross_Cost = PriceofStartTime + PriceofLengthofCall;
    cout<< " Gross cost:"<<gross_Cost<<endl;
    net_Cost = (PriceofStartTime*with_discount) + PriceofLengthofCall;
    cout<< " Net Cost: " <<endl;
    }

    // Only PriceofLengthofCall will be discounted by
    else if (start_time<1800 || start_time>800)
         if (Length_of_call>60)
         { PriceofStartTime = (start_time*federal_tax);
         PriceofLengthofCall = (Length_of_call*regular_rate);
         gross_Cost = PriceofStartTime + PriceofLengthofCall;
    cout<< " Gross cost:"<<gross_Cost<<endl;
    net_Cost = PriceofStartTime + (PriceofLengthofCall*discount_PerMin);
    cout << " Net Cost:" <<net_Cost <<endl;
    }


         // Both PriceofStartTime & PriceofLengthofCall are discounted.
         // because of start_time and call_length
        else if (start_time >=1800 || start_time<800)
             if (Length_of_call>60)
            { PriceofStartTime =(start_time*federal_tax);
            PriceofLengthofCall=(Length_of_call*regular_rate);
            gross_Cost =  PriceofStartTime + PriceofLengthofCall;
        cout << "Gross cost: "<<gross_Cost<<endl;
        net_Cost= (PriceofStartTime*with_discount)+
                    (PriceofLengthofCall*discount_PerMin);
        cout << "Net cost: "<< net_Cost << endl;
        }

           // No discounts are applicable!
           // time and length do not meet discount criteria
          else if (start_time<1800 || start_time>800)
               if (Length_of_call<60)
              { PriceofStartTime =(start_time*federal_tax);
           PriceofLengthofCall=(Length_of_call*regular_rate);
           gross_Cost=  PriceofStartTime + PriceofLengthofCall;
           cout << "Gross cost: "<<gross_Cost<<endl;
           net_Cost = gross_Cost ;
           cout << "Net cost: "<< net_Cost << endl;
           }
           return 0;
           }


My code is not giving the desired output.

You should also give us your incorrect input also. The more detailed your problem is the more you can get help.
Enter start time (0-2400):
2322
Enter the length of the call (minutes):
67
Gross cost:116.33
Net Cost:96.632

Process returned 0 (0x0) execution time : 8.700 s
Press any key to continue.

Output 2:
Enter start time (0-2400):
759
Enter the length of the call (minutes):
10
Gross cost:33.86
Net Cost:

Process returned 0 (0x0) execution time : 17.969 s
Press any key to continue.

closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/193836/#msg932229
Tried both of the solutions suggested there, however the output is still incorrect
closed account (48T7M4Gy)
The one I wrote many months ago gives the correct answer IIRC and I think some (all) of the others do too. However, the point I would make in why I am showing you previous stuff is:
1. It has all been done before so why duplicate effort and waste your time?

2. It is not offered just to copy. You need to follow it and understand if you decide to gain anything by it. The choice is yours. It wasn't meant as a quick fix.

3. You'll see that you can dramatically simplify and clarify by breaking the solution down into simple and more compact steps rather than complex and repetitive structures that I reckon make things hard to follow and pin down why you're not getting the answers you want. Good software design eliminates duplicated code. Each step needs to be tested and checked rather than running the whole program each time. You need to check each business rule is being applied properely by printing out diagnostic sub-answers and messages.

5. While it won't solve much, I'd get rid of float's and use double's also.
This is one of the challenges in my C++ book.I got the program to work but it's definitely super messy and hard to follow. My next goal is to take your advice and simplify it. I went back and did some of the easier examples and used all doubles instead of floats!
Thank you!
-Cristina :)
closed account (48T7M4Gy)
Cheers :)

Good luck with the simplification, it's more satisfying than having so many twists and turns. Come back if you need help.
Topic archived. No new replies allowed.