1

I want to create a function that prompts the user for time, written as HH:MM, where HH is stores in hour_arrived and MM in minute_arrived. However functions cannot return two values, is there a way I can do this?

It has to be a function since this is what is instructed in the assignment, and no built in functions are allowed. And keep in mind I want to use an if function on the time(hours and minutes variables) outside the function.

This was my attempt:

int departTime(int&hour_start, int&min_start){
    string timeD;
    cout<<"Departure Time on the first day of the trip in HH:MM Format: ";
    cin>>hour_start;
    cin.ignore();
    cin>>min_start;
    cin.ignore();

    while((hour_start<0)||(min_start<0)|| ((hour_start>23)||(min_start>59))){
        //START TIME VALIDATION CHECK
        cout<<"Error, invalid time. Please try again."<<endl;
        cout<<"Departure Time on the first day of the trip in HH:MM Format: ";
        cin>>hour_start;
        cin.ignore();
        cin>>min_start;
        cin.ignore();
    }
    timeD = hour_start + ":" + min_start;
    cout<<"Dep time is: "<<timeD;
}
soup
  • 79
  • 5
  • Return an array? – SoWhat May 23 '21 at 18:56
  • 2
    Return a `std::pair` or a `struct Time { int hours, minutes;}`. – molbdnilo May 23 '21 at 18:58
  • 4
    Does this answer your question? [Returning multiple values from a C++ function](https://stackoverflow.com/questions/321068/returning-multiple-values-from-a-c-function) – diviquery May 23 '21 at 19:00
  • `return std::pair;` I added this are the end but it gave me a type/value mismatch – soup May 23 '21 at 19:04
  • Return a vector? Pass a vector to be modified? – Thomas Matthews May 23 '21 at 19:04
  • @soup you cant pass variables in template arguments. Declare the return type as `std::pair` and then use `std::make_pair()` instead, eg: `return std::make_pair(hour_start, min_start);` – Remy Lebeau May 23 '21 at 20:45
  • Please note that you are already passing those variables by *reference* so that they can be changed inside the function. The returned `int` value can be used to express a failure in acquiring the data (EOF, too many wrong inputs) or the success of the operation. – Bob__ May 24 '21 at 08:29

2 Answers2

1

Method 1

Create a class or a struct to store your time.

struct Time
{
    int HH;
    int MM;
};

Method 2

Make the variables storing the time global. See scope.
Modify them inside the function and use them outside.

Method 3

Use reference parameters.
Changes made in the function are reflected outside.

Method 4

Return a pair or a tuple.

Kitswas
  • 1,134
  • 1
  • 13
  • 30
0

You can return a data structure that holds multiple data. For your example, the best way is to use a pair<int, int>& variable. So, make sure to send a pair variable as parameters (also you can use 2 separate variables, but that implies a pair creation inside your function, so the preferred way is with a single pair variable in your case). After you performed what you need, you can return the same pair variable(function return type should be also changed to a pair<int, int>&). Also, I think you would like to send the pair as a parameter, so don't forget to put the & keyword. Here you can find how to use the pair data structure