r/programmingchallenges • • Oct 13 '14

Using in file and an if statement

I am trying to get my code to recognize if what the user types in has a file in that directory. For example if the user types in SportsData.txt and that file is in the directory, I would like it to continue with the if statement, and I'd like the else to say the file wasn't there or something like that. Here is what I have so far.

include <iostream>

include <fstream>

include <string>

using namespace std;

int main ()
{
ifstream inData;
ofstream outData;
string fileName;

cout << "Enter the input filename: ";    
cin >> fileName;    

inData.open("fileName");    

if (inData)    
	cout << "Boom";    
else    
	cout << "It didn't open";    
	

return 0;    

}

2 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/okmkz Oct 14 '14

Agreed, this would better be suited elsewhere. While the sub is pretty small, I'll let the votes decide for the time being.