Reading and writing to txt file from different cpp files.

I've created a cpp and header file called staff. Staff inputs closing and opening hours of a train station. While User can read them.

Code below is Staff writing to a txt file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  void Staff::SelangorOperationHours()
{
	ofstream outputFile;
	outputFile.open("SelangorOperationHours.txt");  //TODO 24 hour clock so user does not type 123131 am

	string SelangorOpening, SelangorClosing;

	cout << "Enter Opening Hours for Selangor Station: ";
	cin >> SelangorOpening;
	outputFile << SelangorOpening << endl;

	cout << "Enter Closing Hours for Selangor Station: ";
	cin >> SelangorClosing;
	outputFile << SelangorClosing << endl;
	
	outputFile.close();
	cout << "Done!\n";
}


Now I would like that user to be able to read know the opening and closing hours, this section is causing me some problems.
Code below reference is from http://www.cplusplus.com/forum/general/55650/

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

void User::ViewOperationHours()
{
	cout << "Select one of the following Railways.\n";
	cout << "1) Selangor Railway \n2) Malacca Railway \n3) Negeri Sembilan Railway \n4) Exit\n";

	int selection;
	cin >> selection;

	
	if (selection == 1)
	{
		string getcontent;
		ifstream openfile("SelangorOperationHours.txt");
		if (openfile.is_open())
		{
			while (!openfile.eof())
			{
				getline(openfile, getcontent);
				cout << getcontent << endl;
			}
		}
	}
}


I don't understand what I'm doing wrong.
One problem is certainly while (!openfile.eof())
Should be.
1
2
3
4
while (getline(openfile, getcontent))
{
  cout << getcontent << endl;
}


Are there any other problems?
I've fixed the while statement but the program still isn't working.
program still isn't working.

Please be a bit more specific.
What's wrong? Crashes, wrong output, no output....
Sorry my bad, it just reaches all the way to were the if statement is suppose to execute then does nothing.

https://imgur.com/a/Yjei6 (heres a link for the output of the program)
What does your file SelangorOperationHours.txt contain ?
That's weird the problem is that the .txt file was empty. I could have sworn that I previously wrote something in the txt file.
Well, thanks for helping out.
Is this maybe related to this thread ?
http://www.cplusplus.com/forum/beginner/220991/
Yes it is did not want to add them into a single question. Though that would cause confusion so I separated them into two questions
Topic archived. No new replies allowed.