Hi, I'm writing a program that will output text and save it into a ".txt" file. I ask the user to to name the output file, but I want to be able to check if they enter it as "*.txt" or simply as the name without the ".txt".
I know I need an if statement to check the last four characters in the string - the ".txt" part. If it has the ".txt" then I can leave the filename alone, and if not, then append ".txt" to the filename.
string
inputfile()
{
string text ="";;
string filename = "";;
ifstream input;
cout << "*** NOTE: File must be in current directory. ***\n\n";
cout << "Enter the filename: " << endl;
cin >> filename;
// Here's where the if statement needs to go.
filename += ".txt"; // This will work if the filename doesn't have the ".txt"
cout << filename;
cin.ignore();
input.open (filename.c_str());
getline (input, text);
input.close();
return (text);
}