How to convert this to use functions, instead of hard code

How would I go about converting the follow code from a former project, into a system that imports information from the hard drive? (I.E Read the txt files that the user writes)I understand I'll need to use fstream. However, how do I go about doing this?

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
#include <iostream>

using namespace std;

int main()

{

char choice;

do {
    
    cout << "Main Menu" << endl;
	cout << "What would you like to read about?" << endl;
	cout << "1. PLACEHOLDER" << endl;
	cout << "2. PLACEHOLDER" << endl;
	cout << "3. PLACEHOLDER" << endl;


	cin >> choice;
	

    
    if (choice=='1') {
		cout << "PLACEHOLDER" << endl;	
	} else if (choice=='2') {
		cout << "PLACEHOLDER" << endl;
	} else if (choice=='3') {
		cout << "PLACEHOLDER" << endl;
	} else {
	    

    }

 
    cout << "Do you want to read up on another topic?" << endl;
    cout << "Press y if you wish to read more, or n if you wish to exit" << endl;

    cin >> choice;


}
 
 
    while (choice != 'n');

    return 0;

}
Something like this?

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

void display_file (string subject)
{   string line;

    subject += ".txt";
    ifstream    fin (subject.c_str());
    while (getline (fin, line))
        cout << line << endl;
}
        
int main()
{   int choice;

    while (true)
    {   cout << "Main Menu" << endl;
	    cout << "What would you like to read about?" << endl;
	    cout << "1. Subject 1" << endl;
	    cout << "2. Subject 2" << endl;
	    cout << "3. Subject 3" << endl;
	    cout << "4. Exit" << endl;
    	cin >> choice;
	    
        switch (choice) 
        {
        case 1:     
            display_file ("subject1"); 
            break;
        case 2:
            display_file ("subject2"); 
            break; 	
	case 3:
	        display_file ("subject3"); 
	        break;
        case 4:
            return 0;	    
	default:
	    cout << "Invalid choice" << endl;    
	}     
        cout << "Do you want to read up on another topic?" << endl;    
    } 
    return 0;
}

Topic archived. No new replies allowed.