Okay, I\'m trying to read text off a file but I want to add a loopto make sure t
ID: 3616665 • Letter: O
Question
Okay, I'm trying to read text off a file but I want to add a loopto make sure the user enters the correct file name. In the loop Ihave if the name is entered correctly it won't run, if the name isentered incorrectly it will run, if the name is entered correctlyafter it enters the loop it won't get out.#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fileName;
ifstream inFile;
cout << "Enter a valid file name:";
cin >> fileName;
inFile.open (fileName.c_str());
while (!inFile)
{
cout<< "Re-enter a vliad file name: ";
cin >>fileName;
inFile.open(fileName.c_str());
}
Explanation / Answer
please rate - thanks don't know why but this seems to work #include #include #include using namespace std; int main() { string fileName; ifstream inFile; cout > fileName; inFile.open (fileName.c_str()); for (;!inFile.is_open();) { cout>fileName; inFile.open(fileName.c_str()); } system("pause"); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.