Question: In the program, each line of the text file is read into an array. I need to have the user input a line number, then that line of the text file will be printed. How is this done? Thanks!

```
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void file()
{
    string array[2990];
    short loop=0;
    string line;
    ifstream myfile ("weblog.txt");
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,line);
            array[loop] = line;
            cout << array[loop] << endl;
            loop++;
        }
        myfile.close();
    }
    else cout << "file not available";

}

int main ()
{
    file();
    return 0;
}

```

Answer: 
I am assuming that you wanted to print the line that the user inputs to so for example if user input line 3, then you will print the third line. If that is so, the following will work.

```
#include<bits/stdc++.h>

using namespace std;

struct OpenFailException : public exception{
    const char* what() const throw (){
        return "Cannot open file";
    }
};

class FileIO{
    public:
        FileIO(const string& Path,const bool &ToMemory){
            _Path = Path;
            _File.open(_Path);
            _Load = ToMemory;

            if(_File.is_open()){
                if(ToMemory){
                    while(!_File.eof()){
                        string Input;
                        getline(_File, Input, '\n');
                        _Memory.push_back(Input);
                    }
                }
            }

            else{
                cout<<"File Err";
                exit;
            }
        }
        string Data(const int &Line){
            return _Memory[Line - 1];
        }
    private:
        string _Path;
        fstream _File;
        vector <string> _Memory;
        bool _Load;
};

int main(){
    FileIO A("CMS.cpp", true);
    int Input;
    cin>>Input;
    cout<<A.Data(Input);
}

```

you can either turn the array into a global variable or you can simply structure a new class for it. Here, I try to abstract the codes for you.
To take in input from an user, what you can do is use the cin function. Below is the reference website.
<http://www.cplusplus.com/reference/iostream/cin/>

Using the cin function you can direct the userinput into a variable, and use that variable to access your array.

Here is an example of the code, assuming your result is zero-indexed (i.e. array[0] is equal to line number 0), if you want your index to start at 1 (i.e. array[0] is equal to line number 1), then just do array[lineNum-1]:

```
int lineNum;
cout << "Please enter a line number";
cin >> lineNum;
cout << array[lineNum];

```