//Write a while clause that tells the computer to stop processing the loop instr
ID: 3635991 • Letter: #
Question
//Write a while clause that tells the computer to stop processing the loop instruction when the end of the file has been reached. The file is associated with the inFile object.This needs to be checked.
<code>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string title = "";
string year = "";
//create file object and open the file
ofstream outFile;
outFile.open("movies.txt", ios::out);
//determine whether the file is open
if (inFile.is_open())
{
//get movie title
cout << "Movie title (-1 to stop): ";
getline(cin, title);
while (title != "-1")
{
//get the release year
cout << "Year released: ";
getline(cin, year);
//write the record to the file
outFile << title << '#' << year << endl;
//get another movie title
cout << "Movie title (-1 to stop): ";
getline(cin, title);
} //end while
//close the file
outFile.close();
}
else
cout << "The file could not be opened." << endl;
//end if
system("pause");
return 0;
} //end of main function
<code>
Thanks
Explanation / Answer
To do that, put a inFile.good() in the while statement. It returns false if the file is bad or you have reached the end of the file. #include #include #include using namespace std; int main() { string title = ""; string year = ""; //create file object and open the file ofstream outFile; outFile.open("movies.txt", ios::out); //determine whether the file is open if (inFile.is_open()) { //get movie title cout Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.