Problem with function

Hi All

Can anybody tell me what is wrong with this function.

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
int inputAndValidate(int entranceHourP , int entranceMinutesP, int exitHourP, int exitMinutesP)
{
/*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).*/

  cout << "Enter the Entrance Hour : ";
  cin >> entranceHourP; 
  cout << "Enter the Entrance Minute : ";
  cin >> entranceMinutesP;
  cout << "Enter the Exit Hour : ";
  cin >> exitHourP;
  cout << "Enter the Exit Minute : ";
  cin >> exitMinutesP;
  

  
  if (exitHourP == 24) 
  {
    exitMinutesP = 0;
    return exitMinutesP;
  }
  if (entranceHourP >= 0 && exitHourP <= 24) 
  {
    cout << entranceHourP;
    return entranceHourP;
    return entranceMinutesP;
    return exitHourP;
    return exitMinutesP;
  }
}


1
2
      inputAndValidate(entranceHour, entranceMinutes, exitHour, exitMinutes);
      cout << entranceHour << " " << entranceMinutes << " " << exitHour << " " << exitMinutes << endl;


I did confirm that the input is correct within the function but as soon as it returns and I do a cout I get the following values 1986174516 2686792 2 37

Any assistance is greatly apreaciated
1
2
3
4
    return entranceHourP;
    return entranceMinutesP;
    return exitHourP;
    return exitMinutesP;

A function can only return one thing. And once it returns, it returns, that is, execution ends when it hits a return statement.
You can only return one value from an function. :/

When you pass variables as arguments, when you declare your function like you did there, you're merely passing the variables' values. If you change its value, the original variable's value will not change with it. What I think would have helped you more is if you passed by reference, so that your function could manipulate the variables' values, like so:
void inputAndValidate(int& entranceHourP , int& entranceMinutesP, int& exitHourP, int& exitMinutesP); //I also changed the return type.

See the &s?

Good luck!

-Albatross
Last edited on
Topic archived. No new replies allowed.