How do I open a user's file in C++? What is wrong with my code?

First of all I NEED to make a function for this program to open the file & I'm supposed to pass the variable by reference. If the user inputs a filename that is not in the directory or it is spelled wrong then I'm supposed to display an output message. I have no clue what I'm doing wrong with this code please help! What is wrong with my code & what is the right code for this?

#include<iostream>
#include<fstream>
using namespace std;

// Function Prototypes
void readFilename(ifstream);

int main()
{
// Variables
ifstream inFile;
string filename;
// Function Calls
readFilename(inFile);

return 0;
}

// Function: readFilename
void readFilename(ifstream &inFile)
{
while{
cout << "Please enter the file name you wish to open." << endl;
getline(cin, filename);
inFile.open(filename.c_str());
if (!inFile)
{
cout << "This file did not open properly and the program will now terminate.\nPlease make sure the spelling of the file is correct." << endl;
}
else break;
}
You missed the & in the function declaration.
void readFilename(ifstream&);
Topic archived. No new replies allowed.