Unit 4: Programming CSIS 123 Programming Fundamentals: The purpose of this lab i
ID: 3908745 • Letter: U
Question
Unit 4: Programming CSIS 123 Programming Fundamentals: The purpose of this lab is to gain practice using control statements. Your friend is interested in being able to sort a media collection. There will be a file containing media, and the request is to accept or delete each entry in the media file. File is attached here Constraints: Do not use any concepts beyond the current chapter, unless explicitly stated in the tasks below Re-use a single string variable for each entry Hint: Examine the input text file. You will be repeating the setps below for each record. Task 1: Setup Variables 1. Add your input text file to the resource files folder 2. Use preprocessor directives to include input/output, file input/output, and string variables 3. Declare the input file variable you will use in place of cin for file input. 4. Declare the output file variable you will use inplace of cout for keep file output. 5. Declare the output file variable you will use inplace of cout for delete file output. 6. Declare a variable to contain movie titles, of type string 7. Declare a variable to contain a user keystroke choice, of type char 8. Separate Task 1 from Task 2 by pressing ENTER on your keyboard. This provides a single line of whitespace in your code Task 2: InputOutput Data 1. Use the variable from Task 1.3 to open the text file in Task 1.1 2. Use the variable from Task 1.3 to assign value to Task 1.6 3. Use a control statement to determine if the variable from Task 1.6 has value 4. If Task 22 returns TRUE, promt to either keep or discard the media identified by the string variable. The user decision will be stored in the variable from Task 1.7 5. Use a control statemnt (cannot be the same kind of control statement from Task 2.2) to read the variable from Task 1.7 If it is the case that the user enters "Y for yes, write variable from Task 1.6 to the variable from Task 1.4 If it is the case that the user enters N for no, write the variable from Task 1.6 to the variable from Task 1.5 If it is the case that the user enters any other variable, notify the user the input is invalid, then wite the variable from Task 1.6 to the variable from Task 1.4 6. Store an empty string to the variable from Task 1.6 7. Repeat Task 2.1- 2.5 for each entry in movie_list.tot 8. Separate Task 2 from Task 3 by pressing ENTER on your keyboard. This provides a single line of whitespace in your code Task 3: Cleanup 1. Close the connection from the variable in Task 2.3 2. Close the connection from the variable in Task 2.4 3. Close the connection from the variable in Task 2.5Explanation / Answer
ScreenShot
-------------------------------------------------------------
Program
/* Task 1*/
//Header files declaration for I/O , file operations and string manipulation
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
//Input output file object creation
ifstream infile;
ofstream ofFileAccept,ofFileReject;
//String to read text file line by line
string movieTitle = "";
//User choice
char ch;
/*Task 2*/
//File names to open
infile.open("movie_list.txt");
ofFileAccept.open("movie_list_accept.txt");
ofFileReject.open("movie_list_reject.txt");
//Check if it present or not
if (!infile) {
cout << "The file not found" << endl;
exit(0);
}
//If present read one by one
else{
while (!infile.eof()) {
infile >> movieTitle;
cout << "Do you wish to catalogue " << movieTitle << "? (y/n):";
cin >> ch;
//If user choice to keep catalogue is yes, then store in one file
if (ch == 'y') {
if (!ofFileAccept) {
cout << "The file not found" << endl;
exit(0);
}
else {
ofFileAccept << movieTitle<<endl;
}
}
//If no,then store in another file
else if (ch == 'n') {
if (!ofFileReject) {
cout << "The file not found" << endl;
exit(0);
}
else {
ofFileReject << movieTitle<<endl;
}
}
//if user enter wrong choice ,shows error message and keep that file
else {
cout << "Invalid Choice , Keeping " << movieTitle;
if (!ofFileAccept) {
cout << "The file not found" << endl;
exit(0);
}
else {
ofFileAccept << movieTitle << endl;;
}
}
}
}
/* Task 3*/
//Close all open objects
infile.close();
ofFileAccept.close();
ofFileReject.close();
//For check purpose display keep and discard movie details
cout << "Movies to keep:" << endl;
infile.open("C:/Users/deept/Desktop/movie_list_accept.txt");
while (infile >> movieTitle)
cout << movieTitle << endl;
infile.close();
cout << endl<<"Movies to discard:" << endl;
infile.open("C:/Users/deept/Desktop/movie_list_reject.txt");
while (infile >> movieTitle)
cout << movieTitle << endl;
infile.close();
return 0;
----------------------------------------------
Output
Do you wish to catalogue Army_of_Darkness_(1992).avi? (y/n):y
Do you wish to catalogue Batman_&_Robin_(1997).m4v? (y/n):n
Do you wish to catalogue Donnie_Darko_mp4? (y/n):y
Do you wish to catalogue Dr._Strangelove_or_How_I_Learned_to_Stop_Worrying_and_Love_the_Bomb_(1964).m4v? (y/n):n
Do you wish to catalogue Life_of_Brian_(1979).avi? (y/n):r
Invalid Choice , Keeping Life_of_Brian_(1979).aviDo you wish to catalogue Lock,_Stock_and_Two_Smoking_Barrels_(1998).avi? (y/n):f
Invalid Choice , Keeping Lock,_Stock_and_Two_Smoking_Barrels_(1998).aviDo you wish to catalogue National_Lampoon's_Christmas_Vacation_2_Cousin_Eddie's_Island_Adventure_(2003).mkv? (y/n):y
Do you wish to catalogue Repo!_The_Genetic_Opera_(2008).mkv? (y/n):n
Do you wish to catalogue Spaceballs_(1987).mkv? (y/n):y
Do you wish to catalogue To_Wong_Foo_Thanks_For_Everything,_Julie_Newmar.mp4? (y/n):n
Movies to keep:
Army_of_Darkness_(1992).avi
Donnie_Darko_mp4
Life_of_Brian_(1979).avi
Lock,_Stock_and_Two_Smoking_Barrels_(1998).avi
National_Lampoon's_Christmas_Vacation_2_Cousin_Eddie's_Island_Adventure_(2003).mkv
Spaceballs_(1987).mkv
Movies to discard:
Batman_&_Robin_(1997).m4v
Dr._Strangelove_or_How_I_Learned_to_Stop_Worrying_and_Love_the_Bomb_(1964).m4v
Repo!_The_Genetic_Opera_(2008).mkv
To_Wong_Foo_Thanks_For_Everything,_Julie_Newmar.mp4
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.