Function Returning

I need to create a function that asks for the user input (1-6) to make a choice. The numbers are linked to a movie so I used a switch to do that but when I go back into the main body the movieName doesn't follow. Please help...

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

void displayHeader();
void displayMovie ();
string getMovieChoice(string);
void displayMovieSchedule();


int main() {
   
    string movieName;
   
    displayHeader();
    displayMovie();
    getMovieChoice (movieName);
    displayMovieSchedule();
    cout<< movieName;
    
    
    return 0;
}

//1
void displayHeader()
{cout<<"•••••••••••••••••••••••••••••••••••••••••••\n\tWelcome to the Movie-Ticket Program\n•••••••••••••••••••••••••••••••••••••••••••\n";}
//2
void displayMovie() {cout<<"\nHere is the movie list:\n\t1-The Invisible Man\n\t2-Impractical Jokers\n\t3-The Call of the Wild\n\t4-Sonic the HedgeHog\n\t5-Fantasy Island\n\t6-Exit\n"; }
//3
string getMovieChoice(string movieName){
    int movieChoice;
    cout<<"Please enter your choice (1-6): ";
    cin>>movieChoice;
    if (movieChoice<1 || movieChoice>6) {
        do{
        cout<<"That choice is invalid.\nPlease enter your choice (1-6): ";
        cin>>movieChoice;
        } while (movieChoice<1 || movieChoice>6);
    }
    if (movieChoice==6) {
               cout<<"\nThank you for using the program\n";
        return 0;
               }
    switch (movieChoice) {
    case 1:
        movieName= "The Invisible Man";
        break;
    case 2:
        movieName="Impractical Joker";
        break;
    case 3:
        movieName="The Call of the Wild";
        break;
    case 4:
        movieName="Sonic the Hedgehog";
        break;
    case 5:
        movieName="Fantasy Island";
        break;}
return movieName;
}

//4

void displayMovieSchedule()
    {
    cout<<"\nYour movie's schedule is:\n\t1-IMAX 1:00-3:00\n\t2-VIP 2:30-4:30\n\t3-ULTRA AVX 2:30-4:30\n\t4-REGULAR 4:45-6:45\n";}
Last edited on
First of all, learn about https://en.wikipedia.org/wiki/Indentation_style .
Pick a style, and stick to it.
Nobody wants to see code where they have to reach for the horizontal scroll bar.

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
83
84
85
86
87
88
89
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void displayHeader();
void displayMovie();
string getMovieChoice(string);
void displayMovieSchedule();

int main()
{
  string movieName;

  displayHeader();
  displayMovie();
  getMovieChoice(movieName);
  displayMovieSchedule();
  cout << movieName;

  return 0;
}

//1
void displayHeader()
{
  cout << "•••••••••••••••••••••••••••••••••••••••••••\n"
          "\tWelcome to the Movie-Ticket Program\n"
          "•••••••••••••••••••••••••••••••••••••••••••\n";
}

//2
void displayMovie()
{
  cout << "\nHere is the movie list:"
          "\n\t1-The Invisible Man"
          "\n\t2-Impractical Jokers"
          "\n\t3-The Call of the Wild"
          "\n\t4-Sonic the HedgeHog"
          "\n\t5-Fantasy Island"
          "\n\t6-Exit\n";
}

//3
string getMovieChoice(string movieName)
{
  int movieChoice;
  cout << "Please enter your choice (1-6): ";
  cin >> movieChoice;
  if (movieChoice < 1 || movieChoice > 6) {
    do {
      cout << "That choice is invalid.\nPlease enter your choice (1-6): ";
      cin >> movieChoice;
    } while (movieChoice < 1 || movieChoice > 6);
  }
  if (movieChoice == 6) {
    cout << "\nThank you for using the program\n";
    return 0;
  }
  switch (movieChoice) {
  case 1:
    movieName = "The Invisible Man";
    break;
  case 2:
    movieName = "Impractical Joker";
    break;
  case 3:
    movieName = "The Call of the Wild";
    break;
  case 4:
    movieName = "Sonic the Hedgehog";
    break;
  case 5:
    movieName = "Fantasy Island";
    break;
  }
  return movieName;
}

//4
void displayMovieSchedule()
{
  cout <<
      "\nYour movie's schedule is:"
      "\n\t1-IMAX 1:00-3:00"
      "\n\t2-VIP 2:30-4:30"
      "\n\t3-ULTRA AVX 2:30-4:30"
      "\n\t4-REGULAR 4:45-6:45\n";
}


> string getMovieChoice(string);
You say it returns a result.
It does return a result.
But you ignore the return result.

what do you mean i ignore it? the result is correct if i cout in the function definition but in the main nothing shows up. and wow yes much cleaner!
> getMovieChoice(movieName);
When you call it, you ignore the result.

Like maybe perhaps
string theMovieYouWantToWatch = getMovieChoice(movieName);

Or since you pass in a variable, perhaps you could make it a reference parameter instead.

Basically, decide whether you're going to return by value - using the return statement and assignment in main, or via a reference parameter.

Last edited on
Got it! thanks
Topic archived. No new replies allowed.