help with the InputAndValidate Function

Would you kindly review the code. I've finished the coding, I'm struggling with one function.

This is the question I got regarding the function.
Function inputAndValidate
This function inputs the time. The time should be entered as two separate values for the hours and minutes,
e.g. if the customer enters the parking garage at 9h30, the time should be entered as 9 for the hour and 30 for the minutes. Do not accept a value of less than 0 or more than 24 for the hour, or a value of less than 0 or more than 59 for the minutes. If this happens a prompting message should be displayed and the values entered repeatedly until they are valid. If a value of 24 has been input for the hour, the minutes should be made 0, regardless of what value the user has entered for the minutes. You have to decide yourself on the type of function and the number and type(s) of the parameter(s).

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
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;
const int MinsInHour = 60;
const float Hourlyfee = 1.50;
const float minfee = 7.00;
const int NoOfCustomers = 10;

void inputAndValidate(int & entranceH, int & entranceM, int & exitH, int & exitM)
{
   do
   {
      cout << "Enter the entry time: ";
      cin >> entranceH >> entranceM;
      cout << "Enter the exit time: ";
      cin >> exitH >> exitM;
   } while(((entranceH < 0) || (entranceM > 24)) && ((entranceM < 0) || (entranceM > 24)) && ((exitH < 0) || (exitH > 24)) && ((exitM < 0) || (exitM > 24)));
   
   if (entranceH == 24)
     entranceM = 0;
     
   if (exitH == 24)
     exitM = 0;
}
void convertToMinutes(int entranceH, int entranceM, int exitH, int exitM, int & entranceTM, int & exitTM)
{
   entranceTM = (60 * entranceH) + entranceM;
   exitTM = (60 * exitH) + exitM;
   
}
void convertBackToTime(int entranceTM, int exitTM, int & parkedH, int & parkedM)
{
   int total = exitTM - entranceTM;
   
   parkedH = total / MinsInHour;
   parkedM = total % MinsInHour;
}
float calcCharge(int parkedH, int parkedM)
{
  float total = 0.0;
  
  if (parkedH >= 3)
  {
    total += minfee;
    total += (parkedH * 1.50);
  }
  else if (parkedH < 3)
         total += parkedH * 1.50;
  else
      total += 1.50;
      
  return total;
}

int main( )
{
  int entranceHour, entranceMinutes;
  int exitHour, exitMinutes;
  int entranceTimeInMins, exitTimeInMins, minutesParked;
  int parkedHours, parkedMinutes;
  float charge, totalCharges;
  
  totalCharges = 0.0;
  cout << "..............PARKING TIME.............." << endl;
  for (int i = 1; i <= NoOfCustomers; i++)
  {
    cout << endl;
    inputAndValidate(entranceHour, entranceMinutes, exitHour, exitMinutes);
    cout << "Entry; " << entranceHour << "H" << entranceMinutes << endl;
    convertToMinutes(entranceHour, entranceMinutes, exitHour, exitMinutes, entranceTimeInMins, exitTimeInMins);
    cout << "Exit: " << exitHour << "H" << exitMinutes << endl;
    convertBackToTime(entranceTimeInMins, exitTimeInMins, parkedHours, parkedMinutes);
    cout << "Parked Time: " << parkedHours << "H" << parkedMinutes << endl;
    charge = calcCharge(parkedHours, parkedMinutes);
    totalCharges += charge;
  
    cout << "Charge: R" << charge << endl;
  }
  
  cout << " Total charge: R" << totalCharges << endl;
  
 return 0;
} 
Last edited on
Topic archived. No new replies allowed.