Question

In: Computer Science

Using the four step (viz., analyze the problem, develop a solution, code the solution, test the...

Using the four step (viz., analyze the problem, develop a solution, code the solution, test the program) program development method, write a C++ program that permits users to enter the following information about your small company’s 5 employees and then writes the information to a file “employee.txt”:

ID No. Sex (M/F) Hourly Wage Years with the Company

Make sure that you open the file for write using the two-step process of “trial read followed by write” so that an existing file with the same name does not get inadvertently clobbered. Close all open files after completing read or write operations. Test your program thoroughly. Using WordPad or Notepad, print out the content of the created file “employee.txt” residing in your project folder. BONUS POINTS will be awarded for input data validation and/or exception handling by “try-throw-catch” construct.

SOLUTION: STEP 1: Analyze the Problem - The problem requires user data entries via “for” loop to be saved in “employee.txt” file.

STEP 2: Develop a Solution - Open the file for dummy “read” (ifstream inFile). If the file exists, will ask the user if overwrite is allowed. If no, close the inFile and exit. If yes, close the inFile and open the file for write (ofstream outFile). Query user for data entry, display on console debug window, and write to the outFile. Close the outFile. After running the program, display the contents of the “employee.txt” file.

STEP 3: Code the Solution

Copy and paste your Visual Studio C++ code here:

STEP 4: Test and Correct the Program

Copy and Paste the display on your Console Debug window here:

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

#include <iostream>
#include <string>
#include <ctime>
#include <math.h>
#include <fstream>
using namespace std;

int main()
{
        try
        {
                ifstream inFile;

                inFile.open("employee.txt");
                if (inFile)
                {
                        cout << "This file already exists.Would you like to overwrite (Y/N) ";
                        char overwrite;
                        cin >> overwrite;
                        if (overwrite == 'Y' || overwrite == 'y')
                        {
                                inFile.close();
                                ofstream outfile;
                                outfile.open("employee.txt");
                                cout << "Please enter all 5 employee id sex(M/F) Hourly wage Years with company one by one\n";

                                for (int i = 0; i < 5; i++)
                                {
                                        string id;
                                        int year;
                                        int hourly;
                                        char sex;
                                        cin >> id >> sex >> hourly >> year;
                                        outfile << id << " " << sex << " " << hourly << " " << year << "\n";
                                }
                                outfile.close();
                        }
                        else
                                return 0;
                }
                else
                {
                        inFile.close();
                        ofstream outfile;
                        outfile.open("employee.txt");
                        cout << "Please enter all 5 employee id sex(M/F) Hourly wage Years with company one by one\n";

                        for (int i = 0; i < 5; i++)
                        {
                                string id;
                                int year;
                                int hourly;
                                char sex;
                                cin >> id >> sex >> hourly >> year;
                                outfile << id << " " << sex << " " << hourly << " " << year;
                        }
                        outfile.close();
                }
        }
        catch (std::ifstream::failure e)
        {
                std::cerr << "Exception opening/reading/closing file\n";
        }
}


Related Solutions

Using Octave, analyze the response for the system to a unit step input. Share the code...
Using Octave, analyze the response for the system to a unit step input. Share the code or commands used. System : x'''+3x''+2x=0
Please provide a step-by-step solution, with "Given:", "Find:", and "Solution:" format, for the problem below. Please...
Please provide a step-by-step solution, with "Given:", "Find:", and "Solution:" format, for the problem below. Please be sure to include any tables, values, and references required to arrive at the solution you provide so that the process may be repeated for other values. Horizontal Formwork Design Given: 6 in structural steel-concrete floor slab. Use ¾ in Class I plyform parallel to the span for the sheathing and Southern Yellow Pine #2 for the rest of the lumber. Joists will be...
Please provide a step-by-step solution, with "Given:", "Find:", and "Solution:" format, for the problem below. Please...
Please provide a step-by-step solution, with "Given:", "Find:", and "Solution:" format, for the problem below. Please be sure to include any tables, values, and references required to arrive at the solution you provide so that the process may be repeated for other values. Vertical Formwork Design Given: Design formwork for a 6-ft-high wall. The concrete is to be placed at a rate of 2 ft/hr and will not be vibrated. Concrete temperature is expected to be 50˚F. Use standard weight...
Using the six step process and Minitab program complete the test for independence: Problem Definition: People...
Using the six step process and Minitab program complete the test for independence: Problem Definition: People of differing ages turn to a variety of media to get their news. A study indicated the various age groups receive their news from the media groups shown in the following contingency table. Medium Under 36 years of age 36-50 years of age Above 50 years of age Local TV 107 119 133 National TV 73 102 127 Radio 75 97 109 Local Newspaper...
 how to run a t-test using STATA step by step?(command)
 how to run a t-test using STATA step by step?(command)
Can I get a Step by Step on how to do this problem Test the claim...
Can I get a Step by Step on how to do this problem Test the claim that the proportion of people who own cats is larger than 80% at the 0.005 significance level. The null and alternative hypothesis would be: H0:p≤0.8H0:p≤0.8 H1:p>0.8H1:p>0.8 H0:μ=0.8H0:μ=0.8 H1:μ≠0.8H1:μ≠0.8 H0:p≥0.8H0:p≥0.8 H1:p<0.8H1:p<0.8 H0:p=0.8H0:p=0.8 H1:p≠0.8H1:p≠0.8 H0:μ≥0.8H0:μ≥0.8 H1:μ<0.8H1:μ<0.8 H0:μ≤0.8H0:μ≤0.8 H1:μ>0.8H1:μ>0.8 The test is: right-tailed left-tailed two-tailed Based on a sample of 700 people, 82% owned cats The test statistic is:  (to 2 decimals) The p-value is:  (to 2 decimals) Based...
Calculate the pH of each aqueous solution: I need step to step solution please using formula...
Calculate the pH of each aqueous solution: I need step to step solution please using formula from; pH = -log [H3O+] pOH = -log[OH-] pH + pOH = 14 1)0.80 M lactic acid and 0.40 M lactate ion. 2) 0.10 mol of formic acid, HCOOH, and 0.10 mol of sodium formate, HCOONa in 1 L of water . 3) 0.30 M NH3 and 1.50 M NH4+
DESCRIPTION Objectives To develop a recursive solution to an open-ended iterative problem To develop a program...
DESCRIPTION Objectives To develop a recursive solution to an open-ended iterative problem To develop a program that manages bad input Description Write a program (printSquares.scala) that read non-negative Int values from a user one at a time. For each positive Int, print out a message with the square of the number. Your program should quit when the use enters a negative Int. Note: You will also need to manage bad input! If the user types something in that isn't an...
1. A clear step by step formulation and solution (using Hamiltonian function) of the two body...
1. A clear step by step formulation and solution (using Hamiltonian function) of the two body problem relative to inertial frame of reference. 2. A clear step by step formulation and solution (using Hamiltonian function) of the two body problem relative to a non-inertial frame of reference.
Please provide a step by step solution Key the names in indexing order using the ARMA...
Please provide a step by step solution Key the names in indexing order using the ARMA rules. In the upper right corner of each card, key the corresponding number for each name James R. Larsen Bob O’Donald Helen Vandermallie Martha Odell-Ryan Sister Catherine George Harris, Ph.D. Mrs. Georgia Harris Father Jenkins Ty Chen Martha Odellman Allens Swap Shop J. T. Larson Herbert Vander Mallie George Harris, M.D. Mary Allen’s Beauty Shop Marshall Field & Company Georgia Harris Allens’ Print Shop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT