In: Computer Science
1 Purpose
This is another program for reading from files. In this program we
will read blocks of data from a (well-formatted) file that you will
be given and make some computations on that data.
The input file will be formatted as described later in this
document. It will be similar the “block” data from Lecture
10.
2 Procedure
You will create one source code program, Prog07.cpp. This program
will
1. Prompt the user for an input filename. Store the filename as a
string object. See Lectures 10 and 11. Then prompt the user for an
output filename. Store it the same way.
2. Open the input filename for reading. Open the output filename for
writing with append. If you cannot open either of the filenames the
user supplied, prompt the user for two more. Repeat this until you
can open the both user’s files. I found that if you want to use just
the plain file name, the file should be placed the directory where
Visual Studio puts the “.vcxproj” file and the “.cpp” file. Otherwise
you have to give the FULL path to the file. If you put the file on
the Desktop, the filename will be something like
C:\users\your_user_name\Desktop\your_real_file_name
3. Once the files are open, read in blocks of data until one of two
things happens:
• You reach the end of the file. • The block you read in has an ID
number of 0.
1
4. The data you are provided will be stored in files using a
structure that is defined like this:
struct employee { int id; char department[25]; float hours;
};
5. For each block of data (unless its ID number is 0) add the
number of hours to compute a total hours.
6. Count the number of employees. Every block with an ID number
greater than 0 is another employee. Do not worry about
duplicates.
7. Once you are through reading, the program will write three lines
of output to the output file (the last line is just blank)
like
Filename.info: 76 employees Total hours: 192.8, Average per
employee: 19.7278 hours.
That should be the actual filename you used NOT “Filename.info.” See
the end of this document for some example runs of the
program.
8. Make sure that you close both the files before exiting.
9. If you have done this correctly, you can reuse the output
filename and see the output of all the tests in it.
10. Upload your solution to the WyoCourses site, Program 07
assignment.
11. Your solution should consist of exactly 3 files:
(a) A C++ source code file, Prog07.cpp which contains all of the
code for the main program as well as the code for all the
functions. (b) A text file, Prog07Test.txt which contains the
results of the tests of your program using all four of the data
files. Remember to test that if you supply incorrect input filenames,
the program prompts again for new filenames. If you give it the
“wrong” output filename, the program will just create a new file. (c)
A text file, Prog07Output.txt which contains the actual program
output of the the four tests. (d) I am not going to require a
pseudocode file this time.
2
Program 07 Tests.
Example results from a completed version of “Prog07.cpp” using only
the “data1.info” and “data2.info” files. Note that “type” is a
command line command that ’dumps’ text files to the terminal. You
could just open the output file in any text editor (I would not use
notepad, but notepad++ works well).
buckner ~...prog07/solution> Prog07.exe
Please enter an input filename: data1.info Please enter an output
filename: output.txt
buckner ~...prog07/solution> Prog07.exe
Please enter an input filename: data2.info Please enter an output
filename: output.txt
buckner ~...prog07/solution> type output.txt
data1.info: 25 employees. total hours: 1221.92, Average per
employee: 48.8769 hours.
data2.info: 15 employees. total hours: 907.893, Average per
employee: 60.5262 hours.
buckner ~...prog07/solution>
what ive got so far....
// Prog07.cpp
// Nick Hill
// COSC1030 lab section 12
// program 07
#include<iostream>
#include<fstream>
#include<string>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ofstream;
using std::ifstream;
using std::string;
using std::getline;
struct employee
{
int id;
char department[25];
float hours;
}emp;
int main()
{
ofstream write;
ifstream read;
string inFile,
outFile;
bool open = 0;
while (!open)
{
cout << "Enter an input
filename: ";
getline(cin, inFile);
cout << "Enter an output
filename: ";
getline(cin, outFile);
read.open(inFile);
write.open(outFile,
std::ofstream::app);
if (read.is_open() &&
write.is_open())
{
open = 1;
}
}
read.eof();
if(!read.eof())
{
}
i cant figure out where to go from here
Since you have not provided input file structure I am assuming that all the data of one structure are in a continuous way. If it does not work please provide input file format.
#include<iostream>
#include<fstream>
#include<string>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ofstream;
using std::ifstream;
using std::string;
using std::getline;
int main()
{
ofstream write;
ifstream read;
string inFile, outFile;
bool open = 0;
while (!open)
{
cout << "Enter an input filename: ";
getline(cin, inFile);
cout << "Enter an output filename: ";
getline(cin, outFile);
read.open(inFile);
write.open(outFile, std::ofstream::app);
if (read.is_open() && write.is_open())
{
open = 1;
}
}
int id;
char data[25];
float hours;
int count = 0;
float total_hours = 0;
while(read >> id && id != 0 && read >> data && read >> hours){
count++;
total_hours += hours;
}
float avg_hours = total_hours / count;
//Getting just the file name
string inputFileName = inFile.substr(inFile.rfind("\\") + 1);
write << inputFileName << ": " << count << " employees. " << "Total hours: " << avg_hours << ", " << "Average per employee: " << avg_hours << " hours" << endl;
read.close();
write.close();
return 0;
}