Need Help on reading in text files

My college professor assigned us a program to complete. I am having trouble with a few of the methods. The first instruction asked that I "1. Use a function named PromptUser() to ask the user for a filelename. It will return the filelename as a string object.
2. Open the filename for reading. If you cannot open the fileename given, prompt the user for another. Repeat this until you can open the user's file.

The skeleton for the function he gave us looked like this

1
2
3
4
5
string PromptUser() 
{
  string filename;
  return filename;
}


I added

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
string PromptUser()
{
  string filename;
  bool working = true;
  
  while(working == true)
  {
    cout << "Please enter a file name: " << endl;

    cin >> filename;

    ifstream inFile(filename);
  
    if(!inFile) 
    {
      cerr << "Could not open: " << filename << endl;
      exit(1);
    }
	else
	{
	  working = false;
	}
  }

  return filename;
}


Am I on the right track or not even close...

I appreciate any help, thanks a ton
Mostly right. The instructions seem to want the promptuser() to just get the filename, so it should just do that. back in your main program you should have the
1
2
3
4
5
6
7
while(!working){ 
 promptUser(); 
 inFile... 
 if(!inf...)
    else 
 working =true;
}
Last edited on
Topic archived. No new replies allowed.