Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this C++ programming project is to allow the student to process i

ID: 3606878 • Letter: T

Question

The purpose of this C++ programming project is to allow the student to process input and output files. This program has the following four menu options: A: Create a new file for Payroll Information B: Enter Payroll Information C: List Payroll Information X: Exit the Payroll Information Module How to Begin Project: 1. Create project P08 2. Add text file output.txt 3. Add text file P08.txt 4. Add C++ source file P08.cpp 5. Copy-and-paste the source code below, and implement options A through C. 6. If the project is built and executed at this point, there will be several warning generated by the compiler, but the program should compile as-is. 7. See the textbook for similar examples. Test Data: Enter the data listed below using option B. Your program will be saving the records in P08.txt as it is entered. 1018 9.50 50 10 25 30 1005 5.00 30 35 40 50 1002 8.00 50 50 10 40 General Processing: See the comments included with the source code provided for hints on what needs to be coded for each function. See the textbook for similar examples. Requirements: Code the createNewFile, enterInfo, and listInfo functions. Turn in: P08.cpp, p08.txt, and sample output (output.txt) from the three options (A, B, and C) Report Layout with Scale: 1 2 3 4 5 6 7 8 12345678901234567890123456789012345678901234567890123456789012345678901234567890 Employee Rate Wk1 Wk2 Wk3 Wk4 1018 9.50 50 10 25 30 1005 5.00 30 35 40 50 1002 8.00 50 50 10 40 Sample Output Option A - Create a new file for Payroll Information: (This option is currently not implemented, but should generate the following.) P08 Main Menu Enter the letter of the desired menu option. Press the Enter key after entering the letter. A: Create a new file for Payroll Information B: Enter Payroll Information C: List Payroll Information X: Exit the Payroll Information Module Choice: a P08 Create new file for Payroll Information Creating a new file will delete any existing information. Do you want to proceed with creating a new file? (Y/N) y New file created successfully! Procedure completed. Press Enter to continue: Sample Output Option B - Enter Payroll Information: (This option is currently not implemented, but should generate the following.) P08 Enter Payroll Information Enter the Employee Id, rate, w1, w2, w3, w4: 1018 9.50 50 10 25 30 Would you like to add another employee? (Y/N) y Enter the Employee Id, rate, w1, w2, w3, w4: 1005 5.00 30 35 40 50 Would you like to add another employee? (Y/N) y Enter the Employee Id, rate, w1, w2, w3, w4: 1002 8.00 50 50 10 40 Would you like to add another employee? (Y/N) n Procedure completed. Press Enter to continue: Sample Output Option C - List Payroll Information: (This option is currently not implemented, but should generate the following.) P08 List Payroll Information Employee Rate Wk1 Wk2 Wk3 Wk4 1018 9.50 50 10 25 30 1005 5.00 30 35 40 50 1002 8.00 50 50 10 40 Procedure completed. Press Enter to continue: Sample Output Option X - Exit: Choice: x Now exiting Payroll Information...please wait. Press any key to continue . . . Source Code: ///P08 File Processing Your Name /* This program allows users to enter and save payroll information to a file. The file can then be read so that the information entered can be displayed. Payroll clerks can choose from the following menu options: A: Create a new file for Payroll Information B: Enter Payroll Information C: Display Payroll Information X: Exit the Payroll Information Module The following items for each employee are saved in the file p08.txt: Employee ID Rate Hours W1,W2,W3,W4 */ #include // file processing #include // cin and cout #include // toupper #include // setw using namespace std; //menu options //Opt A: void createNewFile( ); //Opt B: void enterInfo( ); //Opt C: void listInfo( ); //Supporting functions void displayContinuePrompt( ); //Program starts here void main( ) { //Declare local variable to store menu option selected char choice; //Check to see what the user wants to do do //while (choice != 'X') { cout << "P08 Main Menu "; cout << "Enter the letter of the desired menu option. " << "Press the Enter key after entering the letter. " << " A: Create a new file for Payroll Information " << " B: Enter Payroll Information " << " C: List Payroll Information " << " X: Exit the Payroll Information Module " << "Choice: "; cin >> choice; cout << " "; choice = toupper(choice); //convert to uppercase switch (choice) { case 'A': createNewFile( ); break; case 'B': enterInfo( ); break; case 'C': listInfo( ); break; case 'X': cout << "Now exiting Payroll Information...please wait. "; break; default: cout << ""; //alert cout << "Invalid Option Entered - Please try again. "; }//end of switch } while (choice != 'X'); return; }//end of main //Function Definitions void createNewFile( ) { //Declare local variables char answer; cout << "P08 Create new file for Payroll Information "; //Student must remove next line of code and code the rest of the function. cout << "This option has not been implemented yet."; //Prompt the user if they want to create a new file //If Yes, try to create a new file using open function // Open the file for output // If there are any errors, // display an error message and return. // else // display message that a file was created successfully // Close the file //else // display a message that a new file was NOT created //Call displayContinue to pause displayContinuePrompt( ); return; } void enterInfo( ) { //Declare local variables int employeeId; double rate; int w1Hours, w2Hours, w3Hours, w4Hours; char answer; cout << "P08 Enter Payroll Information "; //Student must remove next line of code and code the rest of the function. cout << "This option has not been implemented yet."; //Open the file for output (append) //If there are any errors, // display an error message and return. //Set output to 2 decimal positions for file //Use a do-while loop to get input // Display prompt and get the data // Write data to file and format using setw manipulators // Prompt user if they want to add another employee // Do loop again if they don't enter N //end do-while loop //Close the file //Call displayContinue to pause displayContinuePrompt( ); return; } void listInfo( ) { //Declare local variables int employeeId; double rate; int w1Hours, w2Hours, w3Hours, w4Hours; cout << "P08 List Payroll Information "; //Student must remove next line of code and code the rest of the function. cout << "This option has not been implemented yet. "; //Open the file for input //If there are any errors, // display an error message and return. //Set output to 2 decimal positions for cout cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); //Display headings cout << "Employee Rate Wk1 Wk2 Wk3 Wk4 "; //Read the first record. //Use while loop to process file, because // while loops handle empty files. // Display record read and format using setw manipulators // Read next record //end while loop //Close the file //Call displayContinue to pause displayContinuePrompt( ); return; } void displayContinuePrompt( ) { //Declare local variables char prompt; cout << " Procedure completed. Press Enter to continue: "; cin.ignore( ); prompt = cin.get( ); system("cls"); //clear screen - DOS return; }

Explanation / Answer

To solve the problem, we will use fstream of C++ to open file for writing, appending and reading.

The following solution source code will help you.

P08.cpp


///P08 File Processing Your Name
/* This program allows users to enter and save payroll information to a file.
The file can then be read so that the information entered can be displayed.
Payroll clerks can choose from the following menu options:
A: Create a new file for Payroll Information
B: Enter Payroll Information
C: Display Payroll Information
X: Exit the Payroll Information Module The following items for each employee are saved in the file p08.txt: Employee ID Rate Hours W1,W2,W3,W4
*/
#include <fstream>
// file processing
#include <iostream>
// cin and cout
#include <string>
// toupper
#include <iomanip>
// setw
using namespace std;
  
//Function Definitions
void displayContinuePrompt( )
{
//Declare local variables
char prompt;
cout << " Procedure completed. Press Enter to continue: ";
cin.ignore( );
prompt = cin.get( );
  
return;
}
  
void createNewFile( )
{ //Declare local variables
char answer;
ofstream file;
cout << "P08 Create new file for Payroll Information ";
//Student must remove next line of code and code the rest of the function.
//cout << "This option has not been implemented yet.";
//Prompt the user if they want to create a new file
//If Yes, try to create a new file using open function
// Open the file for output
// If there are any errors,
// display an error message and return.
// else
// display message that a file was created successfully
// Close the file
//else
// display a message that a new file was NOT created
//Call displayContinue to pause
cout<<"Do you want to create a new file?"<< "Press the Enter key after entering the letter. " << " Y: Yes " << " N: No ";
cin>>answer;
answer = toupper(answer);
switch(answer)
{
case 'Y':
file.open("P08.txt");
  
break;
  
case 'N':
break;
  
default :
cout <<"Invalid option ";
  
}
file.close();
displayContinuePrompt( );
return;
  
}
  
void enterInfo( ) {
//Declare local variables
int employeeId;
double rate;
int w1Hours, w2Hours, w3Hours, w4Hours;
char answer;
cout << "P08 Enter Payroll Information ";
//Student must remove next line of code and code the rest of the function.
  
//Open the file for output (append)
ofstream outfile;

outfile.open("P08.txt", std::ios_base::app);
  
//If there are any errors,
// display an error message and return.
if(outfile.fail())
{
cout<<"Opening File Failed!! ";
cout<<"------------------------------------------ ";
  
cout<<"Possible errors: ";
cout<<"1. The file does not exist. ";
cout<<"2. The path was not found. ";
exit(1); // just exit
// 0-normal, non zero - some errors
}
// else, if the file can be opened
else
{
  
do
{
//Set output to 2 decimal positions for file
//Use a do-while loop to get input
// Display prompt and get the data
// Write data to file and format using setw manipulators
// Prompt user if they want to add another employee
// Do loop again if they don't enter N
//end do-while loop
//Close the file
  
cout<<"Do you want to add a new Employee?"<< "Press the Enter key after entering the letter. " << " Y: Yes " << " N: No ";
cin>>answer;
switch(answer)
{
case 'Y':
cout<<"Enter following data for Employee: EmployeeId, rate, w1 ,w2, w3, w4 ";
cin>>employeeId>> rate>> w1Hours >>w2Hours >> w3Hours >> w4Hours;
outfile << employeeId<<" "<< setprecision(2)<< rate <<" " << setw(2)<< w1Hours <<" " << setw(2) << w2Hours <<" " << setw(2) << w3Hours <<" " << setw(2) << w4Hours<<" ";
break;

case 'N':
break;

default:
cout<<"Invalid Option ";

}

}while(answer!='N');

// close the output file
outfile.close();

// test if fail to close the file, do the following...
if(outfile.fail())
{
cout<<"The file could not be closed! ";
exit(1);
}
// test if successful to close the file, do the following...
else
cout<<" The file was closed successfully! ";
}

  
  
//Call displayContinue to pause
displayContinuePrompt( );
return;
  
}
void listInfo( ) {
//Declare local variables
int employeeId; double rate; int w1Hours, w2Hours, w3Hours, w4Hours,i;
cout << "P08 List Payroll Information ";
//Student must remove next line of code and code the rest of the function. cout << "This option has not been implemented yet. ";
//Open the file for input //If there are any errors, // display an error message and return. //Set output to 2 decimal positions for cout cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
ifstream inFile;
inFile.open("P08.txt");
  
if (!inFile)
{
cerr << "Unable to open file P08.txt";
exit(1); // call system to stop
}
//Display headings
cout << "Employee Rate Wk1 Wk2 Wk3 Wk4 ";
//Read the first record.
//Use while loop to process file, because // while loops handle empty files.
i=0;
while (inFile >> employeeId)
{
inFile >> rate >> w1Hours >> w2Hours >>w3Hours >>w4Hours ;

  
// Display record read and format using setw manipulators
cout << employeeId <<" ";
cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
cout<< rate <<" ";
cout<<w1Hours <<" " <<w2Hours <<" " <<w3Hours <<" " <<w4Hours<<" ";
}
// Read next record
//end while loop
//Close the file
inFile.close();
//Call displayContinue to pause displayContinuePrompt( );
return;
  
}

  
  
//menu options
//Opt A: void createNewFile( );
//Opt B: void enterInfo( );
//Opt C: void listInfo( );
//Supporting functions void displayContinuePrompt( );
//Program starts here
int main( ) {
//Declare local variable to store menu option selected
char choice;
//Check to see what the user wants to do do
//
do
{ cout << "P08 Main Menu ";
cout << "Enter the letter of the desired menu option. " << "Press the Enter key after entering the letter. " << " A: Create a new file for Payroll Information " << " B: Enter Payroll Information " << " C: List Payroll Information " << " X: Exit the Payroll Information Module " << "Choice: ";
cin >> choice;
cout << " ";
choice = toupper(choice);
  
//convert to uppercase
switch (choice)
{ case 'A':
createNewFile( );
break;
case 'B':
enterInfo( );
break;
case 'C':
listInfo( );
break;
case 'X':
cout << "Now exiting Payroll Information...please wait. ";
break;
default: cout << "";
cout << "Invalid Option Entered - Please try again. ";
  
}
//end of switch
} while (choice != 'X');
return 0;
  
}
//end of main
  
P08.txt

122 12.3 12 12 10 66

Output.txt

P08 Main Menu   

Enter the letter of the desired menu option.
Press the Enter key after entering the letter.

A: Create a new file for Payroll Information   
B: Enter Payroll Information   
C: List Payroll Information
X: Exit the Payroll Information Module
Choice: A   


P08 Create new file for Payroll Information   

Do you want to create a new file?Press the Enter key after entering the letter.   

Y: Yes
N: No
Y   


Procedure completed. Press Enter to continue:  

P08 Main Menu   

Enter the letter of the desired menu option.
Press the Enter key after entering the letter

A: Create a new file for Payroll Information   
B: Enter Payroll Information   
C: List Payroll Information
X: Exit the Payroll Information Module   
Choice: B   


P08 Enter Payroll Information   

Do you want to create add a new Employee?Press the Enter key after entering the letter.   

Y: Yes
N: No
Y  
Enter following data for Employee: EmployeeId, rate, w1 ,w2, w3, w4   
12 656 67 67 67 67
Do you want to add a new Employee?Press the Enter key after entering the letter.
Y: Yes
N: No
N

The file was closed successfully!   


Procedure completed. Press Enter to continue:   
P08 Main Menu

P08 Main Menu   

Enter the letter of the desired menu option.
Press the Enter key after entering the letter

A: Create a new file for Payroll Information   
B: Enter Payroll Information   
C: List Payroll Information
X: Exit the Payroll Information Module   
Choice: C

P08 List Payroll Information

Employee Rate Wk1 Wk2 Wk3 Wk4   

12 660.00 67 67 67 67   
P08 Main Menu   

Enter the letter of the desired menu option.
Press the Enter key after entering the letter

A: Create a new file for Payroll Information   
B: Enter Payroll Information   
C: List Payroll Information
X: Exit the Payroll Information Module

Choice: X        Now exiting Payroll Information...please wait.