what application do you use to write a text file that contains a list of element
ID: 3833518 • Letter: W
Question
what application do you use to write a text file that contains a list of elements. And then use said text file in a program to print out the elements to screen?
example question: (program i am writing)
Write a program that reads every line of a text file. The text file contains a list of movies and what they are rated. The user wants to watch a movie with a certain rating. After the program reads the input file with the movies and ratings, the program will ask the user what rating they would like. It will print out a random movie that has the rating (G, PG, PG-13, And R) the user typed. Written in C++.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main() {
ifstream inputFile;
inputFile.open("movie.txt");
string movie, rating;
vector<string> movieV, ratingV;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> movie >> rating;
movieV.push_back(movie);
ratingV.push_back(rating);
}
}
cout<<"Enter the rating: ";
cin >> rating;
for(int i=0;i<ratingV.size() ;i++){
if(ratingV[i]==rating){
cout<<movieV[i]<<" "<<ratingV[i]<<endl;
}
}
inputFile.close();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the rating: G
Movie1 G
Movie4 G
movie.txt
Movie1 G
Movie2 PG
Movie3 PG-13
Movie4 G
Movie5 R
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.