Sequential Access Files Program

Creating a program that lets me save payroll amounts in a sequential access file using a loop. Once I have entered them all they should appear in the file, and I want to make sure I'm doing this right as i'm pretty new to this.
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
27
28
  // Chapter 14 Ex. 19.cpp : Defines the entry point for the console application.
//

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


int main()
{
	double payroll = 0.0;
	ofstream outFile;

	outFile.open("Introductory 19.txt", ios::app);

	if (outFile.is_open())
	{
		while (payroll != -1)
			cout << "Enter payroll (-1 to end)";
		cin >> payroll;
	}
	else
		cout << "Introductory 19.txt could not be opened" << endl;
    return 0;
}

This code

1) Writes nothing to file. That's pretty bad for a programme that's meant to write to file.

2) Has a while loop that will run forever, saying "Enter payroll (-1 to end)" forever and ever. The user won't get to type anything in; the while loop doesn't contain any code for taking input. This is also bad.
Hello Databend,

Repeater has said what I was about to say. The program is a good start, but needs some work.

Checking that the stream is open is good and your code will work, but with an out put file if there is a problem the file will be created. This check works better with an input stream.

I do not understand why people are taught or feel the need to put the entire program in the block of the if statement. Checking that the stream is good is all that should be done. Either you do the if block and bypass the else or you do the else block and exit the program. The program should follow the else block.

I hope that you find this a better way to structure your program. Do not miss the comments in the code:

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
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>  // <--- For line 24.
#include <thread>  // <--- For line 24.

using namespace std;  // <--- Best not to use.


int main()
{
	double payroll = 0.0;
	ofstream outFile;

	outFile.open("Introductory 19.txt", ios::app);

	if (outFile.is_open())  // <--- "if (!outFile.is_open()" will not need the next block or the else.
	{
		std::cout << "\n File is open" << std::endl;
	}
	else
	{
		cout << "Introductory 19.txt could not be opened" << endl;
		std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	while (payroll != -1)  // <--- This is an endless loop that only prints the next line.
		cout << "Enter payroll (-1 to end)";  // <--- only line associated with the while loop.

	cin >> payroll;  // <--- Never reached.

	return 0;
}

This is your code just rearranged it still needs work.

Creating a block for the while loop you will also need to include the "cin" inside the block. As Repeater said you will need to either process this input or write it to the output file.

Hope that helps,

Andy
Thanks for the advice Andy and Repeater, I'll use the way you arranged the code and see if I can finish this.
Last edited on
I think I did it correctly this time

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Chapter 14 Ex. 19.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>  
#include <thread>  
using namespace std;  


int main()
{
	double payroll = 0.0;
	ofstream outFile;

	outFile.open("Introductory 19.txt", ios::app);

	if (outFile.is_open())  // <--- "if (!outFile.is_open()" will not need the next block or the else.
	{
		cout << "\n File is open" << endl;
	}
	else
	{
		cout << "Introductory 19.txt could not be opened" << endl;
		this_thread::sleep_for(chrono::seconds(5));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	while (payroll != -1)  // <--- This is an endless loop that only prints the next line.
	{
		cout << "Enter payroll (-1 to end) ";  // <--- only line associated with the while loop.
		cin >> payroll;  // <--- Never reached.
	}

	outFile << payroll << endl;

	outFile.close();

	system("PAUSE");
	return 0;
}


Anyone wanna confirm for me please?
Last edited on
closed account (E0p9LyTq)
The ios::app flag when opening a file for writing tells the program to append what you output to the end of the file. The more times you run the program, the more data is stored to the file.

Because your statement to write the payroll number to the file is outside your while loop, only one value is ever written to the file, and that is -1.
Last edited on
Topic archived. No new replies allowed.