Need help with my C++ Assignment

This is my assignment:
Program Specifications
Write a program that will read movie titles from files and echos (or prints) them to common input.
The program should have a separate function to perform each of the following tasks.
1. Prompt the user for data file name and get the filename.
2. Read one line at a time from the specified file and store all lines in an array and close the file.
3. After all lines are read into the array, the program should print out all the lines of a file from the array.
Hint:
1. Make an array large enough to hold the most movies (i.e. 100)
2. Use the ifstream for the input file type
3. Use the getline function to read lines into a string variable
4. Each string in an array is represented by the array name follow by index in a pair of square bracket.
5. The user should have the option of entering a different file if they choose. Do not end the program making it
work with only one file. (You will need a loop for this. A menu style would be useful for this).
6. Test it using 2 files with the second file having more lines than the first file. You should make sure that if you
read a long file followed by a short file, the file from the first long file does not merge with the new short
file.

This is what I have so far. Any help would be greatly appreciated!
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
  #include <iostream>
#include <string>
#include <fstream>

using namespace std; 

const int size = 100;

void specifyFile(string movies[size], int size);
void storeArray(string movies[size], int size);
void printArray(string movies[size], int size);

int main()
{

	string movies[size];
	
	specifyFile (movies, size);
	storeArray (movies, size);
	printArray (movies, size);
	
	return 0;
}

void specifyFile(string movies[], const int size)

{
	char again;
	
	do
	
	{		
	
	char fileName[100];

	cout << "Please enter a filename " << endl;   
	   
	cin >> fileName;    
	
	storeArray(movies, size);
	
	printArray(movies, size);
	
	cout << "Do you want to enter another text file (Y/N) ";
	
    cin >> again;
    
   } while (again == 'Y' || again == 'y');

}

void storeArray(string movies[], const int size)
{
	char fileName [100];
	
	ifstream in_file;

	in_file.open (fileName);
	
	for (int i = 0; i < size; i++)
	{
		in_file >> movies[i];
	}
	
	in_file.close();

}

void printArray(string movies[], const int size)
{

}
Do you have a question? Are you stuck on something?
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
#include <iostream>
#include <string>
#include <fstream>

// read lines from the file into the array; return the number of lines read
std::size_t get_lines( std::istream& stm, std::string array[], std::size_t max_lines )
{
    std::string line ;

    // for each line read from the file, up to a maximum of max_lines lines
    std::size_t cnt = 0 ;
    while( cnt < max_lines && std::getline( stm, line ) )
    {
        array[cnt] = line ; // store the line in the array
        ++cnt ; // and increment the count
    }

    return cnt ; // return the number of lines that were read
}

// print out the first n_lines lines in the array
void print_lines( const std::string array[], std::size_t num_lines )
{
    std::cout << "\n---- contents of the array ----\n" ;
    for( std::size_t i = 0 ; i < num_lines ; ++i )
        std::cout << i+1 << ". " << array[i] << '\n' ;
    std::cout << "------------- end -------------\n" ;
}

// ask the user if another file is to be read
bool read_another_file() // return true in the answer is yes
{
    std::cout << "\n\nread another file (y/n)? " ;
    char c ;
    std::cin >> c ;
    return c == 'y' || c == 'y' ;
}

int main()
{
    do
    {
        // Prompt the user for data file name and get the filename.
        std::string file_name ;
        std::cout << "file name? " ;
        std::cin >> file_name ;

        // open the file for input
        if( std::ifstream file{file_name} ) // if the file was opened
        {
            // Make an array large enough to hold the most movies (i.e. 100)
            static const std::size_t MAX_MOVIES = 100 ;
            std::string movies[MAX_MOVIES] ;

            // read lines from the file into the array; keep track of number of lines read
            const std::size_t num_movies = get_lines( file, movies, MAX_MOVIES ) ;

            // print out all the lines that were read from the array.
            print_lines( movies, num_movies ) ;
        }

        else std::cout << "can't open file '" << file_name << "'\n" ;
    }
    while( read_another_file() ) ; // repeat the loop if the user wants another file to be read
}
joe, i know how to get the user to input a filename, and how to insert into the array but i get lost with the part where my instructor says to use a loop to create a menu and the getline part

jl borges, thanks for the help but i am a beginner and don't know how to read the code without using namespace std on the top :(
Get the code to work with one file first, then add the loop and menu so it works with multiple files. The comments that follow apply to getting it to work with one file.

What should specifyFile() do? Right now it's all over the place, prompting for the file, storing the file, printing the file, prompting for a new file. Most of that logic belongs in main(). SpecifyFile() should probably just prompt for the file name and return it. That means it should probably return a string. What parameters does it need to take? My advice is to start by defining the parameters, return value, and functionality (in English) of SpecifyFile(). Write the functionality in a comment before the function so you don't get confused about what it should do.

StoreArray() should read the file and store the lines into the array. Keep in mind that you need to pass back the number of items stored in the array. Again, what parameters must be passed to StoreArray() and what must be returned from it? Again, define the parameters, return value, and function. Document it in a comment.

printArray() should just print the array. I think you have the right parameters and return value there.

Now fill in the code. Test it out.

When you're done, you should be able to modify the program by adding code to main() that will do the loop for multiple files.
Registered users can post here. Sign in or register to post.