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)
{
}
|