Problem with returning values from a function

closed account (jwC5fSEw)
I'm making a struct that holds information about a rented video. I want a function that asks the user which format he'd like (e.g: 1. VHS, 2. DVD, etc), takes the number corresponding to the format, and returns the number. Here's all my code so far (currently it's a little bit out of sort, as I'm just testing this one function before I move on):

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
#include <iostream>
using namespace std;

struct video{
    char name[50];
    int releaseYear,
    rentalLength,
    format;
    float rentalCost;

    void getRentalInfo(char name[], int releaseYear, int rentalLength, int format){
        cout << "What is the name of the movie you'd like to rent?\n";
        cin.getline(name,50);
        cout << "What year was it released?\n";
        cin >> releaseYear;
        cout << "How many days would you like to rent it for?\n";
        cin >> rentalLength;
        format = getFormat(format);
//      rentalCost = getRentalCost(rentalLength,format);  not defined yet
    }

    int getFormat(int format){
        while(true){
            cout << "What movie format would you like to rent?\n" <<
                    "1. VHS\n" <<
                    "2. DVD\n" <<
                    "3. Blu-ray\n";
            cin >> format;
            switch(format){
                case 1:
                    cout << "You have chosen to rent a VHS.";
                    return 1;
                case 2:
                    cout << "You have chosen to rent a DVD.";
                    return 2;
                case 3:
                    cout << "You have chosen to rent a Blu-ray.";
                    return 3;
                default:
                    cout << "You have not selected a valid choice.\n";
                    break;
            }
        }
    }
};

int main(){
    video t;
    t.getFormat(t.format);
    cout << endl << t.format;
    return 0;
}


getFormat() works alright until it tries to return the number. I want the value in t.format to be 1, 2, or 3, but instead it returns 2009288233. Can anybody tell me why?
It is returning the number, you just aren't using it. On line 49 you aren't setting what it is returning to anything.

I *think* what you meant to do is make it a void function and pass the int by reference.
How did you verify that getFormat() worked alright? The variable t is not initialized so at the moment you try to call the function getFormat (on line 49) its content is undefined.

What is the argument t.format meant for? There is no need for supplying an argument. Remove the argument and try to call the function in this way: cout << t.getFormat() << endl;
Last edited on
closed account (jwC5fSEw)
Changing it to t.getFormat() worked. I'm still new to member variables and functions, and I wasn't entirely clear on whether or not I needed to put format in the argument list so I did. Removing it got it working. However, would making this void and passing format by reference work better?
In case you need to return two or more values from a function then it's essential to use either references or pointers.

In your case there is only one return value so return statement is sufficient.
closed account (jwC5fSEw)
Okay, thanks. Just to clarify: any function that doesn't end with "return x" is a void function?
It has to be declared as a void function. For example:

1
2
3
4
5
void MyFunction(int & RefToMyVar)
{
   RefToMyVar = 5;
   // no return statement
}
closed account (jwC5fSEw)
Got it, I understand functions much better now. Thanks!
My pleasure. :-)
Topic archived. No new replies allowed.