Review: Long-Distance Calls

Hello, I am having trouble completing this code on Long_Distance Calls, i'll post
the code i have done so far


/*A long-distance carrier charges the following rates for the telephone calls:
______________________________________________________________________
Starting Time of Call Rate per Minute
----------------------------------------------------------------------
00:00 - 06:59 0.12
07:00 - 19:00 0.55
19:01 - 23:59 0.35
----------------------------------------------------------------------
Write a program that asks for the starting time and the number of minutes of the call,
and displays the charges. The program should ask for the time to be entered as a floating-
point number in the form HH.MM. For example, 07:00 hours will be entered as
07.00, and 16:28 hours will be entered as 16.28.
Input Validation: The program should not accept times that are greater than 23:59.
Also, no number whose last two digits are greater than 59 should be accepted. Hint:
Assuming num is a floating-point variable, the following expression will give you its
fractional part:
num - static_cast<int>(num) */
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double time,
		   minutes,
		   rate;

	cout << setprecision(2) << fixed;
	cout << " What is the time? " << endl;
	cout << " Enter the time in this format: HH.MM, HH is hours and \n";
	cout << " MM is minutes:   ";
	cin >> time;
	if(time < 00.00 || time > 23.59)
	{
		cout << " Please enter a time between 00.00 hours and 23.59 hours." << endl;
		system("pause");
		return 0;
	}
	//time - static_cast<int>(time);
	/*if(time > 59)
	{
		cout << " The minutes in HH.MM cannot exceed 59." << endl;
		system("pause");
		return 0;
	}		*/

	cout << "\n How many minutes was your call?  ";
	cin >> minutes;
	cout << " I will display the charges depending on when the call was made\n";
	cout << " and how many minutes the call was." << endl;
	if(time >= 00.00 && time <= 06.59)
	{
		rate = 0.12;
		cout << " The rate of the starting time of call is " << rate << "." << endl;
	}
	else if(time >= 07.00 && time <= 19.00)
	{
		rate = 0.55;
		cout << " The rate of the starting time of call is " << rate << "." << endl;
	}
	else if(time >= 19.01 && time <= 23.59)
	{
		rate = 0.35;
		cout << " The rate of the starting time of call is " << rate << "." << endl;
	}


	system("pause");
	return 0;
}

I am have a problem with the statement of not accepting the last two digits in the time to not exceed 59, please help
Use (a) a do-while loop to make sure that the user input hours and minutes are valid and (b) the modf function (include header cmath) that splits a float into its integer and fraction parts.
Variable minutes had been declared, but not used, in the program. I suppose you wanted to show the total call charge as well based on rate and minutes, so that's been included.
The program also had a return 0 statement on line 20 and another on line 27 (the latter being commented out) - these statements make main() return, thereby ending your program. You can keep the final return 0 statement (line 52) if you wish but that's not strictly necessary.
Finally, take some time to understand how cout formats output to console, right now you have a fair bit of extra stuff in these statements.
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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	float time;
	double minutes, hours, rate;

	do
    {
        cout << "Enter the time in this format: HH.MM\n";
        cin>> time;
        minutes = modf(time, &hours) * 100;
        if(minutes < 0 || minutes > 59 || hours < 0 || hours > 23)
        {
            cout<<"Incorrect time entered, try again \n";
        }
	} while (minutes < 0 || minutes > 59 || hours < 0 || hours > 23);

	cout << "How many minutes was your call?\n";
	cin >> minutes;
    if(time >= 00.00 && time <= 06.59)
	{
		rate = 0.12;
		cout << "Call rate per min:" << rate << "\n";
		cout<<"Call charge: "<<rate * minutes<<"\n";
	}
	else if(time >= 07.00 && time <= 19.00)
	{
		rate = 0.55;
		cout << "Call rate per min:" << rate << "\n";
		cout<<"Call charge: "<<rate * minutes<<"\n";
	}
	else if(time >= 19.01 && time <= 23.59)
	{
		rate = 0.35;
		cout << "Call rate per min:" << rate << "\n";
		cout<<"Call charge: "<<rate * minutes<<"\n";
	}
}
Last edited on
Topic archived. No new replies allowed.