long distance calls

I want to let everyone know this is not homework (as this is only my 4th chapter in this book and if it was homework this program would not be at the end of a semester since it in in May). I am trying to learn C++ on my own and have two questions. My first question is the following code returns the startTime and say for example I hit 7 for my start time I need it to return 07.00 for my start time and it currently returns 7.00.

1
2
3
4
5

//Show results
cout << fixed << showpoint << setprecision(2);
cout << "\nThe start time of the call was " << startTime << endl;


My second question is I need to set the minutes to not go above 59. I know how, for example, how to get the entire 23.59 to not go above .59 but not just the minutes. I am guessing I need to break the start time up into hour and minutes but not sure how to do that.

I also want to add this chapter is just if statements and next chapter is the actual looping so I dont want to do anything that isnt covered in the book up to this point (find something using ceil but it is not covered in the book yet).

Here is my complete code...

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

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

int main()
{
	float startTime;
	double charges;
	int startHour, startMinute, minutes;

	cout << "What was the start time of the call? ";
	cin >> startTime;
	
	if (startTime > 24.00 || startTime < 0)
	{
		cout << "\nInvalid start time!\n\n";
		return 0;
	}

	cout << "How many minutes was the call for? ";
	cin >> minutes;

	if (startTime < 7)
	{
		charges = minutes * .12;
	}
	else if (startTime >= 7 && startTime <= 19)
	{
		charges = minutes * .55;
	}
	else if (startTime >= 19.01 && startTime <= 23.59)
	{
		charges = minutes * .35;
	}

	//Show results
	cout << fixed << showpoint << setprecision(2);
	cout << "\nThe start time of the call was " << startTime << endl;
	cout << "The charges for the call were " << charges << endl << endl;

	return 0;
}
I've come up with solutions like this

1
2
3
4
5
if (startTime > 24.00 || startTime < 0 || (startTime - (int)startTime) > 0.59)
{
	cout << "\nInvalid start time!\n\n";
	return 0;
}



...


1
2
3
4
5
cout << fixed << showpoint << setprecision(2);
cout << "\nThe start time of the call was ";
if(startTime < 10.00) cout << '0';
cout << startTime << endl;
cout << "The charges for the call were " << charges << endl << endl;


Hey rich thanks for the help. I just want to make sure I understand the first code snippet you sent for future reference.

[code]


(startTime - (int)startTime) > 0.59)

[/code

If I am reading this right this is a cast that subtracts the double start time by just the integer part of the start time and leaving the decimal as is?
yes
Topic archived. No new replies allowed.