I want the user to be able to input a file name and display the information within the file. I’ve used the ifstream to read the variable but it only returns an error. I implemented the error so that I could know when the program has found the file or not. I don’t know if the variable needs to be wihtin the folder of the program. Tbh i dont know how to modify it for it to work.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
cout<< "What is your first name?";
cin >> f_user;
cout<< "What is your last name?";
cin >> l_user;
string file_name= f_user + l_user + ".txt";
ifstream inFile(file_name);
if(inFile.is_open()){
string c;
while (getline(inFile, c)){
cout<< c;
}
}
if(inFile.fail()){
cout << "User not Found"<< endl;
}
inFile.close();
]
New contributor
seraphim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.