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) 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.
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:
#include <iostream>
#include <fstream>
#include <string>
#include <chrono> // <--- For line 24.
#include <thread> // <--- For line 24.
usingnamespace 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.
// Chapter 14 Ex. 19.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>
usingnamespace 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;
}
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.