I have a coding assignment that has multiple file options to be opened and read, however if a file that isn't one that my teacher has given us tries to be inputted, an error message should be displayed and the user should be reprompted. I'm having trouble setting my program so that only the files that my teacher has given can be read.
I can honestly say that I have never seen anything like line 4 and I do not believe that you can open a file stream that way.
I would be interested in seeing the whole code Because I am thinking that you need to do something after an input from the user to check the file name.
I am thinking of putting the filenames in an array of std::string and then check the user input against this array. If a match continue on if not deal with the error.
Here is a quick thought. I may come up with something better later, but this will give you an idea of what I am thinking.
#include <iostream>
#include <string>
int main()
{
constexpr size_t MAXSIZE{ 3 };
std::string fileNames[MAXSIZE]{ "File1.txt", "File2.txt", "File3.txt" };
std::string fileName;
bool goodFile{ false };
do
{
std::cout << "\n Enter a file name (File1.txt, File2.txt, File3.txt): ";
std::getline(std::cin, fileName);
for (size_t lc = 0; lc < MAXSIZE; lc++)
{
if (fileNames[lc] == fileName)
{
goodFile = true;
break;
}
}
if (!goodFile)
std::cout << "\n Wrong file name. Try again." << std::endl;
} while (!goodFile);
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
@OP I don't mean to be rude, but - how on earth did you think your line 4 could possibly work?
If your approach to programming is "type any old thing in, and hope that the compiler magically deduces what I was really thinking of" then you might as well give up programming now, because you will never, ever be able to program that way.
The language has rules. If you don't bother learning those rules, then you won't ever learn to program.
The flipside to that is that, if you do learn those rules, you'll discover what a wonderfully powerful and expressive language it is, and you'll be able to intuitively do all sorts of interesting and useful things with it.
Right, now that's out of the way, here's what your program needs to do:
1) Store a list of allowed filenames
2) Allow the user to specify a filename
3) Determine whether that file name is in the list of allowed filenames
4a) If it isn't in the list, then display an error message and reprompt the user to specify the filename
4b) If it is in the list, open the file.
EDIT: Huh... I somehow didn't see Andy's post before I wrote this. His code doesn more or less what I've said. However, I still think you should try and write it yourself rather than copy his - after all, how will you ever learn if you don't try it yourself?