C++: Should I be using the clear function?

May 19, 2009 at 19:39:12
Specs: Windows Vista
Hi, this is my first post here, so please be understanding and forgiving if I screw up and break some rule or something. I'm having trouble figuring out exactly how to remedy this problem in my C++ program. What I want to do is prompt the user to enter a filename from which data will be read. If the filename entered doesn't match the name of a real/pre-existing file, I want the program to output a message saying so and continue prompting the user to input a filename until a valid filename is entered.

If I run the program and enter a valid filename on the first go-round, everything works fine and the file is read. However, if I first enter the name of a nonexistent file and on the second try enter the valid filename, the program displays the same error message as if I had entered an invalid filename.

I tried resetting the variable "filename" to an empty string: filename = ""; But I wasn't sure if this was the right way to do it or not, and also it didn't help. Then I tried using cin.clear() and cin.ignore(), but these didn't make a difference either.


Code (I've omitted pieces where they aren't directly relevant):

ifstream in;
int n;
string filename;
bool validFile = false;
bool fileRead = false;

while (validFile == false)
cout << "Please enter the name of the file you would like to read from." << endl;
cin >> filename;
if (in.fail())
cout << "Sorry, the file name you entered is not valid." << endl;
cout << "Please check the name of the input file and try again." << endl;
validFile = false;
cin.ignore(100000, '\n');

(blah blah blah, it reads the file here. the rest of the code isn't relevant)

If it matters, I'm using Microsoft Visual Studio 2008.

Thank you for your help.

See More: C++: Should I be using the clear function?

Report •

May 21, 2009 at 08:30:06
Do you ever set validFile to true?

Report •

May 21, 2009 at 17:56:55
You need to reset in, not cin

cin.ignore(100000, '\n');
Better version:

#include <limits>
 . . . . .
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

while (validFile == false)
Know your libraries. This will limit the amount of functionality you duplicate.

while (!in.is_open())

Report •

May 21, 2009 at 20:34:42
Razor, thank you. That fixed the problem completely. I guess
I misunderstood exactly where the problem was coming from.
I was thinking it was somehow still trying to open the file with
the first filename entered. So there's just a problem with the
input stream? Thanks again. You're awesome.

Report •

Related Solutions

May 21, 2009 at 20:36:30
Klint, yes, I set validFile to true in the "else" statement, which
I didn't include here because it wasn't relevant and it was
pretty long.

Report •

May 22, 2009 at 02:19:57
By the way, if the user enters a file name that contains spaces, your stream extraction -

cin >> filename;

- will not get the complete file name, but only the first word of it. Consider using getline() instead. This may not be a problem for you, but it may be useful to know for the future.

Report •

Ask Question