All pastes #222079 Raw Edit

Miscellany

public cpp v1 · immutable
#222079 ·published 2006-10-26 01:28 UTC
rendered paste body
///P08 File Processing   Juan Marquez  /*   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 <cctype>   // toupper#include <iomanip>  // setwusing namespace std;//menu options//Opt A: void createNewFile();//Opt B: void enterInfo();//Opt C:    void listInfo();//Supporting functionsvoid 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   Juan Marquez    \n\n";        cout << "Enter the letter of the desired menu option. \n"             << "Press the Enter key after entering the letter. \n\n"             << "  A: Create a new file for Payroll Information  \n"             << "  B: Enter Payroll Information  \n"             << "  C: List Payroll Information   \n\n"             << "  X: Exit the Payroll Information Module \n\n"             << "Choice: ";        cin >> choice;        cout << "\n\n";        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.\n\n";                break;            default:                cout << "\a"; //alert                cout << "Invalid Option Entered - Please try again. \n\n";        }//end of switch    } while (choice != 'X');    return; }//end of main//Function Definitionsvoid createNewFile(){//Declare local variables    char answer;    cout << "P08   Juan Marquez    Create new file for Payroll Information \n\n";//Student must remove next line of code and code the rest of the function. //Prompt the user if they want to create a new file	cout << "Would you like to create a new file? Type Y or N.\n\n";    cin  >> answer;//If Yes, try to create a new file using open function	if (toupper (answer) == 'Y')	{//  Open the file for output 			ofstream outfile;		outfile.open("P08.txt");		//  If there are any errors, //     display an error message and return.		if (outfile.fail())		{			cout << "There was an error.\n";			exit(1);		}//  else //     display message that a file was created successfully//else 				else cout << "New file created successfully!\n"; //  display a message that a new file was NOT created    if (toupper (answer) == 'N')		{			cout << "No new file created.\n";			exit(1);		}//   Close the file				outfile.close();		return;	//Call displayContinue to pause    displayContinuePrompt();    return;}}void enterInfo(){//Declare local variables    int employeeId;    double rate;     int w1Hours, w2Hours, w3Hours, w4Hours;    char answer;    cout << "P08   Juan Marquez    Enter Payroll Information \n\n";//Student must remove next line of code and code the rest of the function.     cout << "Please enter your payroll information.";	//Open the file for output (append) 		ofstream outfile;		outfile.open("P08.txt", ios::app);//If there are any errors, //  display an error message and return.				if (outfile.fail())		{			cout << "An error has occured.\n";					exit(1);		}//Set output to 2 decimal positions for file    outfile.setf(ios::fixed);    outfile.setf(ios::showpoint);    outfile.precision(2);//Use a do-while loop to get input	do //  Display prompt and get the data		{		cout << "P08 Aaron harper enter payrol information.\n\n";		cout << "Enter the Employee Id, rate, w1, w2, w3, w4: \n\n";			cin  >>     			employeeId >> rate >> w1Hours >> w2Hours >> w3Hours >> w4Hours;//  Write data to file and format using setw manipulators		outfile << employeeId << rate << w1Hours << w2Hours << w3Hours << w4Hours;//  Prompt user if they want to add another employee		cout << "Would you like to add another employee (Y/N)\n";		cin >> answer;	//  Do loop again if they don't enter N		}while (toupper (answer) == 'Y');//end do-while loop    cout << endl; //Close the fileoutfile.close(); return;}//Call displayContinue to pause    displayContinuePrompt();  return; void listInfo();{//Declare local variables    int employeeId;    double rate;     int w1Hours, w2Hours, w3Hours, w4Hours;    cout << "P08   Juan Marquez    List Payroll Information \n\n";//Open the file for input  		ofstream infile;		infile.open("P08.txt", ios::app);//If there are any errors, //  display an error message and return.if (outfile.fail())		{			cout << "An error has occured.\n";					exit(1);		}//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 \n\n";//Read the first record. 	 	 infile >> rate >> wk1 >> wk2 >> wk3 >> wk4;	 cin    >> rate >> wk1 >> wk2 >> wk3 >> wk4;//Use while loop to process file, because //  while loops handle empty files.while (! infile.eof() ){	outFile << rate << wk1 << wk2 << wk3 << wk4;	//  Display record read and format using setw manipulators//  Read next record    	inFile >> rate >> wk1 >> wk2 >> wk3 >> wk4;	//end while loop     return 0; }//Close the file    	outfile.close();		return;//Call displayContinue to pause    displayContinuePrompt();    return;}void displayContinuePrompt(){//Declare local variables    char prompt;    cout << "\n\nProcedure completed. Press Enter to continue: ";    cin.ignore();    prompt = cin.get();    system("cls");   //clear screen - DOS    return;}//End of program