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):
#include <iostream>
usingnamespace 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?
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;
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?