Question

In: Computer Science

1 Purpose This is another program for reading from files. In this program we will read...

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

Solutions

Expert Solution

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;

}


Related Solutions

b. Copy the text below into your program. /* * Purpose: Read a 0V-5V value from...
b. Copy the text below into your program. /* * Purpose: Read a 0V-5V value from analog pin A1 * and convert it to a 10-bit (0-1023) number. */ void setup() { Serial.begin(115200); //set serial comm to 115 kbps } void loop() { int pot_val; //input - potentiometer value // analogRead() converts 0V-5V to 0-1023 pot_val = analogRead(1); //read from Analog pin 1 Serial.println(pot_val); //print to serial monitor } c. Add the function delay() from lab0 to make the readings...
What opportunity costs did you incur in reading this chapter? If you read another chapter today,...
What opportunity costs did you incur in reading this chapter? If you read another chapter today, would your opportunity costs (per chapter) increase? Explain. (LO 01-01) Why did both presidents Obama and Trump reduce spending on America’s space exploration program? (LO 01-02) Can we continue to produce more output every year? Is there a limit? (LO2-3) Why are incomes so much more unequal in poor nations than in rich ones? (LO2-4) With respect to the demand for college enrollment, which...
Modify the Assignment program from Module 1 to read a user's first and last name read...
Modify the Assignment program from Module 1 to read a user's first and last name read three integer scores, calculate the total and average determine the letter grade based on the following criteria - Average 90-100 grade is A, 80-89.99 grade is B, 70-79.99 grade is C, all others F (use a nested if) Output formatted results consisting of the full name, the three scores, the total, average (to 2 decimal places), and the letter grade go to step 1...
Purpose To develop C++ code that incorporates: (1) generalized conversion from one number base to another;...
Purpose To develop C++ code that incorporates: (1) generalized conversion from one number base to another; (2) string character extraction and insertion; and (3) a loop trifecta (while, for, and do loops). After this exercise you should better understand the difference between integer and string versions of a number, and the difference between integer and printable character versions of a single digit. Conversions between string and integer representations of a number are needed since you can't perform arithmetic directly on...
The closest distance a book can be read from a pair of reading eyeglasses (Power =...
The closest distance a book can be read from a pair of reading eyeglasses (Power = 2.06 dp) is 27.5 cm. What is the near distance? (Assume a distance between the eyeglasses and the eyes to be 3.00 cm)
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
Read through the assigned reading, Lessons from Nature: A Bio-Inspired Approach to Molecular Design: 1. Describe...
Read through the assigned reading, Lessons from Nature: A Bio-Inspired Approach to Molecular Design: 1. Describe the key design features that led to the development of the picket-fence porphyrin. 2. Give two examples of how hydrogen bonds effect the properties of metal complexes
Dr. Addison hypothesizes that going through a training program will increase weekly reading. College students read...
Dr. Addison hypothesizes that going through a training program will increase weekly reading. College students read a mean of 2.4 days a week with a standard deviation of 1.9 days. Dr. Addison's sample of 28 students read a mean of 2.5 days a week. What can be concluded with an α of 0.10? a) What is the appropriate test statistic? ---Select--- na / z-test / one-sample t-test/ independent-samples t-test / related-samples t-test b) Population: ---Select--- students / individuals exposed to...
Dr. Page believes that going through a training program will decrease weekly reading. College students read...
Dr. Page believes that going through a training program will decrease weekly reading. College students read a mean of 2.3 days a week with a variance of 0.49 days. Dr. Page's sample of 31 students read a mean of 1.9 days a week. What can be concluded with α = 0.01? a) What is the appropriate test statistic? ---Select--- na z-test one-sample t-test independent-samples t-test related-samples t-test b) Population: ---Select--- (reading) college students days in the week individuals exposed to...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT