Limiting user inputs for entering minutes in a billing type format

I apologize if this has been asked before. I tried googling an answer and came up dry. Also when I used the search box in this forum it took me out of the forum section of the site and into the reference section. Is there another way to search just the forums?

Right up front this is a homework question. I think I have everything in it correct except for one part. The problem has the user entering in a time in the format of HH.MM

Since there is only 59 minutes in an hour I need to limit the input to numbers between 0 and 59.

In my book it says to use num - static_cast<int>(num)

I have no idea where in the code to put this or exactly how it should look. The book says "Assuming num is a floating-point variable, the following expression will give you its fractional part". Would "num" relate to my "start_time" variable and how would I incorporate this into the program?

Thank you

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
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    
    float start_time;   //What time the call started
    int minutes;        //How many minutes the call lasted
    double rate1;       //Per minute charge from 00:00 to 06:59
    double rate2;       //Per minute charge from 07:00 to 12:59
    double rate3;       //Per minute charge from 13:00 to 19:00
    double rate4;       //Per minute charge from 19:01 to 23:59
    double cost;        //Total cost of the call
    
    rate1 = .12;
    rate2 = .23;
    rate3 = .55;
    rate4 = .35;
    
    cout << "Please enter what time your call started\n";
    cout << "Please enter in the form of HH.MM" << endl;
    cin >> start_time;
    if (start_time <= 00.00 || start_time >= 23.59) {
        cout << "You have entereed an invalid time, please restart the program and try again";
        return 0;
    }
   
    cout << "Please enter how many minutes the call lasted\n";
    cin >> minutes;
    
    if (start_time >= 00.00 && start_time <= 06.59)
    {
        cost = rate1 * minutes;
        cout << "Your call will cost $" << fixed << showpoint << setprecision(2) << cost;
        
    }
    else
        if (start_time >=07.00 && start_time <= 12.59)
        {
            cost = rate2 * minutes;
            cout << "Your call will cost $" << fixed << showpoint << setprecision(2) << cost;
        }
    else
        if (start_time >= 13.00 && start_time <= 19.00)
        {
            cost = rate3 * minutes;
            cout << "Your call will cost $" << fixed << showpoint << setprecision(2) << cost;
            
        }
    else if (start_time >=19.01 && start_time <= 23.59)
        {
            cost = rate4 * minutes;
            cout << "Your call will cost $" << fixed << showpoint << setprecision(2) << cost;
        }

    
    return 0;

In my book it says to use num - static_cast<int>(num)

I have no idea where in the code to put this or exactly how it should look. The book says "Assuming num is a floating-point variable, the following expression will give you its fractional part". Would "num" relate to my "start_time" variable and how would I incorporate this into the program?


Do you understand what the static_cast<int>(num) is actually doing?

no, I do not.
Okay, that static cast is converting a floating point number to an int which truncates the fractional part. So what you end up with is the whole part of the floating point number.

So if your number is 5.67 your equation would equivalent to 5.67 - 5 which equals .67.
Last edited on
Also I myself would consider changing the way you're getting the starting value. I would use a string to retrieve this value from the user then use a stringstream to parse the string, after I did some initial validation.


1
2
3
4
5
6
7
8
9
10
11
12
13
    string input_value;
    cout << "Please enter what time your call started\n";
    cout << "Please enter in the form of HH:MM" << endl; // Note the colon instead of the decimal point.
    cin >> input_value;
    int start_hours, start_min;
    char colon;
    if(input_value.find(':') == std::string::npos || input_value.length() != 5)
    {
         // Report error and fix the issue or abort program.
    }
    istringstream sin(input_value);
    sin >> start_hours >> colon >> start_min;  



Thanks for clearing that up.

So I should put in something like this?

start_time - static_cast<int>(start_time)

Where would I put that into the code and how does that limit them to only putting in numbers between 0 and 59?
You would assign this value (multiplied by 100) to an int that would give you the start minutes that you would use throughout the rest to the program. In other words convert the floating point number into two integers one integer represents the start hours the other representing the start minutes.


Topic archived. No new replies allowed.