Suggestions for Shortening Code

This is a small beginner project for my self-taught C++ course. It's a movie recommender in which the user selects a number, tied to a genre, and the program reads through a text file and selects a random title for the user. As a beginner my first instinct was to use IF statements but it was way too long. I tried using a SWITCH statement to try to shorten it but it's still the same lenght (although much more organized and easier to read). Any other suggestion for shortening my code, I know I can do better.

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
#include <iostream>
#include <fstream>
#include <string>
#include <limits> 

using namespace std;

int main ()
{

    int number;

    cout << "\n";
    cout << "Welcome!\n \n";
    cout << "What kind of movie are you looking for? \n \n";
    cout << "1)Romance \n2)Comedy \n3)Sci-Fi \n4)Action \n5)Modern Classic \n \n";
    cout << "Select a number: ";
    cin >> number;
    cout << "\n";

    switch (number){
        case 1:{
            string getcontent;
            fstream openfile ("romance.txt");
            if (openfile.is_open())
            {
            getline (openfile, getcontent);
            cout << "Today's recommendation is: " << getcontent << endl;
            }
            break;
        }
        case 2:{
            string getcontent;
            fstream openfile ("comedy.txt");
            if (openfile.is_open())
            {
            getline (openfile, getcontent);
            cout << "Today's recommendation is: " << getcontent << endl;
            }
            break;
        }
        case 3:{
            string getcontent;
            fstream openfile ("scifi.txt");
            if (openfile.is_open())
            {
            getline (openfile, getcontent);
            cout << "Today's recommendation is: " << getcontent << endl;
            }
            break;
        }
        case 4:{
            string getcontent;
            fstream openfile ("action.txt");
            if (openfile.is_open())
            {
            getline (openfile, getcontent);
            cout << "Today's recommendation is: " << getcontent << endl;
            }
            break;
        }
        case 5:{
            string getcontent;
            fstream openfile ("classics.txt");
            if (openfile.is_open())
            {
            getline (openfile, getcontent);
            cout << "Today's recommendation is: " << getcontent << endl;
            }
            break;
        }
        default:
            cout << "Type a number between 1 and 5." << endl;
    }

   return 0;
}
As you see, all of the switch statements contain similar code. You should make a function:
1
2
3
4
5
6
7
8
9
void print_whatever(const char *filename) {
    string getcontent;
    fstream openfile ( filename );
    if (openfile.is_open())
    {
        getline (openfile, getcontent);
        cout << "Today's recommendation is: " << getcontent << endl;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
    ...
    switch(number) {
     case 1:
        print_whatever("romance.txt");
        break;
     case 2:
        print_whatever("comedy.txt");
        break;
     ...
    }
    ...
}
Last edited on
Ah! I knew there was a possibility with so much repetition, I will make functions my friends from now on. Thank your for the lesson!
One more possibility, a slightly different approach. Store all the different filenames in an array of strings. Then instead of using a switch statement, simply retrieve the corresponding filename from the array and then proceed as before.
This is what Chervil means:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void print_whatever(const char *filename) {
    ...
}

int main() {
    ...
    //create array with all the file names (in order)
    const char* filenames[] = {
        "romance.txt",
        "comedy.txt",
        ...
    };
    //bounds checking: number is between 1 and 5 (inclusive)
    if( 1 <= number && number <= 5 )
        //"number is 1" corresponds to "array element 0", etc...;
        print_whatever( filenames[number - 1] );
    else
        //bad input, it's out of bounds
        cout << "Type a number between 1 and 5." << endl;
    ...
}
Topic archived. No new replies allowed.