please simplify this program coding
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: please simplify this program coding

  1. #1
    Join Date
    Aug 2005
    Posts
    22

    please simplify this program coding

    Hi i recently been helped to solve a given problem which is to develop a program to compute the weekly pay for each employees.

    This is the question...

    A company pays its employees as
     managers (who receive a fixed weekly salary),

     hourly workers (who receive a fixed hourly wage for up to the first 40 hours they work and "time and a half" ie. 1.5 time their hourly wage, for overtime hours worked),

     commission workers (who receive a $250 plus 5% of their gross weekly sales), or

     pieceworkers (show receive a fixed amount of money per item for each of the items they produce - each piece worker in this company works on only one type of item). Write a program to compute the weekly pay for each employee.

    The manager asks you to develop a program to compute the weekly pay for each employee, the number of employees paid each week and the total amount paid by the company in salaries that week. The number of employees and hours worked varies from week to week depending on the amount of work to be done. Employees have their own employee number. Each type of employee has its own pay code: Managers have paycode 1, hourly workers have code 2, commission workers have code 3 and pieceworkers have code 4. Use an if-then-else statement (a switch structure is not allowed) to compute each employee's pay based on that employee's pay code. Then, prompt the user (ie the payroll clerk) to enter the appropriate facts your program needs to calculate each employee's pay based on that employee's paycode.

    Sample input/output

    Enter paycode (-1 to end): 1
    Manager selected.
    Enter weekly salary: 2500
    The manager's pay is $2500.00

    Enter paycode (-1 to end): 2
    Hourly worker selected.
    Enter the hourly salary: 10.50
    Enter the total hours worked: 75
    Worker has worked 35.0 overtime hours.
    Workers pay is $971.25

    Enter paycode (-1 to end): 3
    Commission worker selected.
    Enter gross weekly sales: 9000
    Commission Worker's pay is $700

    Enter paycode (-1 to end): 4
    Piece worker selected.
    Enter number of pieces: 200
    Enter wage per piece: 10
    Piece Worker's pay is $2000.00

    Enter paycode (-1 to end): -1

    Number of managers paid: 1
    Number of hourly workers paid: 1
    Number of commission workers paid: 1
    Number of piece workers paid: 1

    Total amount paid in salaries = $6171.25



    I recently been helped but then it's a switch structure (which is not allow)
    this is the code...it work perfectly fine but could someone please help me to solve this problem using an if-then-else statement? preffered to use #include <iostream> and cout << to display output

    this is a working program code using the switch structure......
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	/* pay code */
    	int payCode;
    
    	/* input cariables */
    	double hourSalary = 0.0;
    	double weekSalary = 0.0;
    	double Sale;
    	int numberPieces;
    	double pieceRate;
    	double hourWork;
    	double otHours;
    	double salaryTemp;
    
    	/* counters  */
    	int salaryWorkers = 0,
    		hourlyWorkers = 0,
    		commWorkers = 0,
    		pieceWorkers = 0;
    
    	/* total accumulator for the entire payroll */
    	double totalPaid = 0.0;
    
    	/* input promt  */
    	char *prompt = "\n\t1 - Manager\n\t2 - Hourly\n\t3 - Commission\n\t4 - Piece Worker\n\t -1 to Exit\n\tEnter Code: ";
    
    
    	/* loop till -1 */
    
    	printf(prompt);
    	scanf("%d", &payCode);
    
    	while (payCode != -1)
    	{
    		switch (payCode)
    		{
    
    		case 1:
    			/* weekly salary, more 0  */
    			while (weekSalary <= 0.0)
    			{
    				printf("Enter weekly salary: ");
    				scanf("%lf", &weekSalary);
    			}
    				++salaryWorkers;
    				printf("The manager's pay is %8.2f \n", weekSalary);
    				totalPaid += weekSalary;
    				break;
    
    		case 2:
    			/*  hourly rate, more than 0 */
    			while (hourSalary <= 0.0)
    			{
    				printf("Enter the hourly salary: ");
    				scanf("%lf", &hourSalary);
    			}
    				++hourlyWorkers;
    				printf("Enter the total hours worked: ");
    				scanf("%lf",&hourWork);
    
    
    			/* over 40 hours  */
    			if (hourWork > 40)
    				{
    					otHours = hourWork - 40;
    					salaryTemp = ((40 * hourSalary) + (1.5 * otHours * hourSalary));
    				}
    				else
    				{
    					salaryTemp = hourWork * hourSalary;
    				}
    
    				totalPaid += salaryTemp;
    				printf("Workers pay is %8.2f \n", salaryTemp);
    				break;
    
    			case 3:
    				++commWorkers;
    				printf("Enter gross weekly sales: ");
    				scanf("%lf",&Sale);
    				salaryTemp = Sale * .05 + 250.0;
    				totalPaid += salaryTemp;
    				printf("Commission Worker's pay is %8.2f \n", salaryTemp);
    				break;
    
    			case 4:
    				++pieceWorkers;
    				printf("Enter number of pieces: ");
    				scanf("%lf",&pieceRate);
    
    				printf("Enter wage per piece: ");
    				scanf("%d",&numberPieces);
    				salaryTemp = pieceRate * numberPieces;
    				printf("Piece Worker's pay is %8.2f \n", salaryTemp);
    				totalPaid += salaryTemp;
    				break;
    
    	default :
              printf("Please enter 1, 2, 3, 4 or -1\n");
              break;
    
    		}
    
    		printf(prompt);
    		scanf( "%d", &payCode );
    	}
    
    	printf("The total paid out this week is $%11.2f \n", totalPaid);
    
    	/* dislay results  */
    	printf("Number of Managers paid: %d\n", salaryWorkers);
    	printf("Number of Hourly Workers paid: %d\n", hourlyWorkers);
    	printf("Number of Commission Workers paid: %d\n", commWorkers);
    	printf("Number of Piece Workers paid: %d\n", pieceWorkers);
    
    	return 0;
    }
    Last edited by Ejaz; August 19th, 2005 at 03:20 PM. Reason: Code tag added

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: please simplify this program coding

    And why isn't a switch statement not allowed?

    Code:
    switch(value)
    {
     case cond1: ... break;
     case cond2: ... break;
     case cond3: ... break;
     case cond4: ... break;
     default:
    }
    cand be replaced with
    Code:
    if(value == cond1)
    {
    }
    else if(value == cond2)
    {
    }
    else if(value == cond3)
    {
    }
    else if(value == cond4)
    {
    }
    else
    {
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Aug 2005
    Posts
    22

    Re: please simplify this program coding

    Thank for the help.......but then again as i am a newbie i have no idea how to change or apply your code into the program.......and is there a way to actually simplify the whole entire program into a more simplier coding using #include <iostream> and cout << coding with out using this part of the coding

    #include <stdio.h>

    int main(void)
    {
    /* pay code */
    int payCode;

    /* input cariables */
    double hourSalary = 0.0;
    double weekSalary = 0.0;
    double Sale;
    int numberPieces;
    double pieceRate;
    double hourWork;
    double otHours;
    double salaryTemp;

    /* counters */
    int salaryWorkers = 0,
    hourlyWorkers = 0,
    commWorkers = 0,
    pieceWorkers = 0;

    /* total accumulator for the entire payroll */
    double totalPaid = 0.0;

    /* input promt */
    char *prompt = "\n\t1 - Manager\n\t2 - Hourly\n\t3 - Commission\n\t4 - Piece Worker\n\t -1 to Exit\n\tEnter Code: ";


    /* loop till -1 */

    printf(prompt);
    scanf("%d", &payCode);

    while (payCode != -1)
    {


    where i have absolutely no idea what the program is doing and what each individual code line means......in other word, so far i only been learning up to the stage where i only know how to use #include <iostream> and cout << to program....could anyone simplify the program codes so that a newbie like myself can follow along and understand the coding of this program?

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: please simplify this program coding

    Quote Originally Posted by tx8koibito
    where i have absolutely no idea what the program is doing and what each individual code line means......in other word, so far i only been learning up to the stage where i only know how to use #include <iostream> and cout << to program....could anyone simplify the program codes so that a newbie like myself can follow along and understand the coding of this program?
    Then I suggest start learning, and write programs with statement that you know, not copy them from others. As you can see, that doesn't help you.

    And please use code tags when posting code.
    Last edited by cilu; August 20th, 2005 at 08:15 AM.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jul 2004
    Posts
    142

    Re: please simplify this program coding

    hello tx8koibito

    There are two goals in a homework assignment:
    1) Produce code.
    2) Learn how to do the homework.

    So if you say "I don't know what the code does"
    then your homework also is
    to learn what every line does, in detail.

    Then the next step is, you must understand how it all works.

    Then the next step is, you make it better.

    The point is, that you must understand it.
    Suppose we do your homework assignment for you.
    You would deliver good homework,
    but you would still not know how to program.

    So when the next homework came, and you had to write a program,
    what would you do? You must learn to do it yourself.

  6. #6
    Join Date
    Aug 2005
    Posts
    15

    Re: please simplify this program coding

    If you are new to programming, then that code is way too complicated for you.

    Start from nothing - really. The only way to learn how to program is to always always write the whole thing for yourself at first. Otherwise you are gonna let lines of code slip past you that you have no idea what they are doing. This is where errors and mistakes and misunderstandings will always come from. Not to mention a confused look when your teacher says 'oh, this is interesting, why did you use this?' and you have absolutely no idea what it does, let alone why it is better than another way.

    I presume you have seen the typical 'hello world' application.

    Code:
    #include <iostream>
    using namespace std; //this is used to avoid the need for std::cout later, you can used <<cout. 
    
    main() 
    {
    
    cout << "Hello World" << endl; //send 'hello world' to the screen, endl starts a new line. 
    
    return(); //end the function
    }
    That is the simplest program, ever. You say you understand how <iostream> and cout are used. Do you understand what they actually mean? Saying #include <iostream> tells the compiler 'I am using functions that you can find in iostream.h'. One of those is <<cout - it writes stuff out to the screen. Another is >>cin. >>cin takes user input from the keyboard, to a variable you select.

    Code:
     
    int myvariable;
    cout << "enter a number" << endl;
    cin >> myvariable;
    after the above code is run, whatever the user has entered, will be stored in myvariable for you to use as you wish. To test this you could add the line 'cout << myvariable;' to check. This is fine if they enter a number, if they enter text things will go wrong. Try it and see for yourself what happens if they enter text instead of a number. This will teach you that you need to check what users put in, make sure it is 'good'. Look up cin.good. Learn how to use it, and try it.


    The code you pasted, uses stdio.h, instead of iostream. This contains a whole load of similar functions. The equivalent of <<cout is printf(), the equivalent of >>cin is scanf().

    Before you can understand how these have been used in the code you supplied, you need to understand pointers. This, you should look up, in a book or whatever. When you've looked it up, you should try writing some small programs - OF YOUR OWN - which use pointers, and see if you can make things work.

    Some of this stuff you may already know, and I appologise if I sound patronising. But I'm trying to show you the processes you should go through if you want to learn to program, or if you are trying to understand a section of code you are not familiar with.

    Good luck.

  7. #7
    Join Date
    Aug 2005
    Posts
    22

    Re: please simplify this program coding

    thank for the tips guys.....but as u see....I am trying to learn bases on that code....not really intend to use that code without understanding it.........

    i seem to understand the rest of the code...but i dont really know what the

    Double function do.....

    could any explain to me why there is the double function in the code??

  8. #8
    Join Date
    Aug 2005
    Posts
    104

    Re: please simplify this program coding

    double is a variable type, not a function.
    It's a "double-precision" floating point number.

  9. #9
    Join Date
    Aug 2005
    Posts
    22

    Re: please simplify this program coding

    oh is that so......but why do we use double variable in this case? is there a reason for it? i mean cant we just us Float instead?

    now it seem to me that

    printf("Enter weekly salary: "); is the same as cout << which is to display the resul on screen,

    but what about

    canf("%lf", &weekSalary);

    i couldnt figure out what this statement mean.....and why it is used.


    futher on let say i attemp to use if-the-else statement how would i go about not using canf("%lf", &weekSalary);

  10. #10
    Join Date
    Jul 2004
    Posts
    142

    Re: please simplify this program coding

    tx8koibito,
    trying to learn C by asking one Codeguru question at a time is
    going to take you a long long time.

    What's your next question going to be? Are you
    going to ask what does + do?

    If you do not even know type declarations like "double"
    then you should sit down and read, cover-to-cover,
    any good book on C or C++

  11. #11
    Join Date
    Aug 2005
    Posts
    22

    Re: please simplify this program coding

    Ok......this is my attempt on the problem using if-then-else statement

    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <iomanip>
    using namespace std;

    int main()
    {
    // Paycode 1-4 or -1
    int paycode;

    // input variables
    double hourSalary = 0.0;
    double weekSalary = 0.0;
    double Sale;
    int numberPieces;
    double pieceRate;
    double hourWork;
    double otHours;
    double salaryTemp;

    // counters
    int salaryWorkers = 0,
    hourlyWorkers = 0,
    commWorkers = 0,
    pieceWorkers = 0;

    //total accumulator for the entire payroll
    double totalPaid = 0.0;

    //Prompt
    cout<< "Enter a Paycode (-1 to End): ";
    cin >> paycode;

    do
    {
    if (paycode == 1)
    {
    ++ salaryWorkers;
    cout<< "Manager Selected"<<endl;
    cout<< "Enter weekly salary: ";
    cin >> weekSalary;
    cout<< "The Manager's Pay is "<<"$"<<weekSalary<<endl;
    }


    if (paycode == 2)
    {
    ++ hourlyWorkers;
    cout<< "Hourly Worker Selected"<<endl;
    cout<< "Enter the hourly salary: ";
    cin >> hourSalary;
    cout<< "Enter the total hours worked: ";
    cin >> hourWork;

    if (hourWork > 40)
    {
    otHours = hourWork - 40;
    salaryTemp = ((40 * hourSalary) + (1.5 * otHours * hourSalary));
    }
    else
    {
    salaryTemp = hourWork * hourSalary;
    }
    cout<< "Worker has worked "<< otHours << " overtime hours."<<endl;
    totalPaid += salaryTemp;
    cout<< "Workers pay is "<<"$"<< salaryTemp<<endl;
    }


    if (paycode == 3)
    {
    ++ commWorkers;
    cout<< "Commission Worker Selected"<<endl;
    cout<< "Enter gross weekly sales: ";
    cin >> Sale;
    salaryTemp = (Sale * .05) + 250.0;
    totalPaid += salaryTemp;
    cout<< "Commission Worker's pay is "<<"$"<< salaryTemp<<endl;
    }


    if (paycode == 4)
    {
    ++ pieceWorkers;
    cout<< "Piece worker selected "<<endl;
    cout<< "Enter number of pieces: ";
    cin >> numberPieces;
    cout<< "Enter a wage per piece: ";
    cin >> pieceRate;
    printf("Enter wage per piece: ");
    salaryTemp = (pieceRate * numberPieces);
    cout<< "Piece Worker's pay is "<<"$"<< salaryTemp<<endl;
    }


    if (paycode == -1)
    {
    cout<< "Number of managers paid: "<< salaryWorkers<<endl;
    cout<< "Number of hourly workers paid: "<< hourlyWorkers<<endl;
    cout<< "Number of commission workers paid: "<< commWorkers<<endl;
    cout<< "Number of piece workers paid: "<< pieceWorkers<<endl;
    totalPaid = salaryTemp;
    cout<< "Total amount paid in Salaries= "<<"$"<< totalPaid<<endl;
    }
    }
    while (paycode != -1);

    cin.get();
    return 0;
    }



    it's working.....but then again........there is a problem.......which is everytime u complete a calculation, it doesnt shows up the ENTER PAYCODE (-1 to end): menu so that u can calculate another transaction...

    could you help me resolve this?

  12. #12
    Join Date
    Aug 2005
    Posts
    15

    Re: please simplify this program coding

    Code:
    //Prompt
    cout<< "Enter a Paycode (-1 to End): "; //ONE
    cin >> paycode;//TWO
    
    do
    {//THREE
    .
    .
    .
    .
    }while (paycode != -1);//FOUR
    do-while loop: a do-while loop 'does' whats in the loop 'while' whats in the ( ) is true. In simplistic terms - programs are sequential unless you use loops. So, this program will do what I've labeled as 1, 2, 3, 4, repeat from 3 if 4 is true, otherwise carry on with whatever is next.

    So, if you want your prompt to appear at the beginning of the section of code you want repeating, where should you put it?

    If you can't figure that out, get a book and learn the basics, otherwise you are never going to get anywhere.

  13. #13
    Join Date
    Aug 2005
    Posts
    22

    Re: please simplify this program coding

    Now....This is my final attemp on this problem...........

    #include <iostream>
    #include <fstream>
    #include <iomanip>

    using namespace std;

    int main()
    {
    int paycode = 0;
    int numberPieces = 0;
    float hrsSalary = 0;
    float weeklySalary = 0;
    float weeklySales = 0;
    float pieces = 0;
    float hourWorked = 0;
    float overtimeHours = 0;
    float salaryTemp = 0;

    int salaryWorkers = 0,
    hourlyWorkers = 0,
    commWorkers = 0,
    pieceWorkers = 0;

    float totalpaid = 0;


    cout <<"\n\t1 - Manager:" << endl;
    cout <<"\n\t2 - Hourly:" << endl;
    cout <<"\n\t3 - Commission:" << endl;
    cout <<"\n\t4 - Piece Worker:" << endl;
    cout <<"\n\t-1 to Exit:" << endl;

    cout <<"Enter paycode (-1 to end): ";
    cin >> paycode;

    while (paycode!=-1)
    {
    if (paycode > 4)
    {
    cout << "\n\tPlease enter 1, 2, 3, 4 or -1\n" << endl;
    }
    else
    if (paycode < -1)
    {
    cout << "\n\tPlease enter 1, 2, 3, 4 or -1\n" << endl;
    }
    else
    if (paycode == 1)
    {
    cout << "Manager Selected" << endl;
    cout << "Enter weekly salary: ";
    cin >> weeklySalary;
    salaryWorkers++;
    cout << fixed << setprecision(2) << "The Manager's Pay is $" << weeklySalary << endl;
    totalpaid += weeklySalary;
    }
    else
    if (paycode == 2)
    {
    cout << "Hourly Worker Selected" << endl;
    cout << "Enter the hourly salary: ";
    cin >> hrsSalary;
    hourlyWorkers++;
    cout << "Enter the total hours worked: ";
    cin >> hourWorked;

    if (hourWorked > 40)
    {
    overtimeHours = hourWorked - 40;
    salaryTemp = ((1.5 * overtimeHours * hrsSalary) + (40 * hrsSalary));
    }
    else
    {
    salaryTemp = hourWorked * hrsSalary;
    }
    cout << fixed << setprecision(2) << "Worker has worked "<< overtimeHours << " overtime hours." << endl;
    totalpaid += salaryTemp;
    cout << fixed << setprecision(2) << "Workers pay is $" << salaryTemp << endl;
    }

    if (paycode == 3)
    {
    cout << "Commission Worker Selected" << endl;
    commWorkers++;
    cout << "Enter gross weekly sales: ";
    cin >> weeklySales;
    salaryTemp = weeklySales * .05 + 250.0;
    totalpaid += salaryTemp;
    cout << "Commission Worker's pay is $" << salaryTemp << endl;
    }
    else
    if (paycode == 4)
    {
    cout << "Piece worker selected " << endl;
    pieceWorkers++;
    cout << "Enter number of pieces: ";
    cin >> numberPieces;
    cout<< "Enter wage per piece: ";
    cin >> pieces;
    salaryTemp = (pieces * numberPieces);
    cout << fixed << setprecision(2) << "Piece Worker's pay is $" << salaryTemp<<endl;
    totalpaid += salaryTemp;
    }

    cout <<"\n\t1 - Manager:" << endl;
    cout <<"\n\t2 - Hourly:" << endl;
    cout <<"\n\t3 - Commission:" << endl;
    cout <<"\n\t4 - Piece Worker:" << endl;
    cout <<"\n\t-1 to Exit:" << endl;

    cout <<"Enter paycode (-1 to end): ";
    cin >> paycode;


    if (paycode == -1)
    {
    cout<< "Number of managers paid: "<< salaryWorkers<<endl;
    cout<< "Number of hourly workers paid: "<< hourlyWorkers<<endl;
    cout<< "Number of commission workers paid: "<< commWorkers<<endl;
    cout<< "Number of piece workers paid: "<< pieceWorkers<<endl;

    cout<< "Total amount paid in Salaries= "<<"$"<< totalpaid<<endl;
    cin.get();
    }
    }
    cin.get();
    return 0;
    }




    Ok....im glad everything work just fine......however......i could only prevent the user from entering numbers <-1 and number >4.............and display error message.......but then i couldnt prevent user from inputting character like "a" b or c....

    could anyone help me to prevent user from entering characters and display the error messgage?

  14. #14
    Join Date
    Aug 2005
    Posts
    15

    Re: please simplify this program coding

    Quote Originally Posted by sammiantha
    Look up cin.good.

    As I said way back - you need to look at cin.good() for validating user input.

  15. #15
    Join Date
    Jun 2005
    Posts
    241

    Re: please simplify this program coding

    another hint: if you are not sure what a function or varable does like "doulbe" just goto google and type "c++ doulbe" and there would be many sites that would answer your questions very fast or also search Codeguru and see if you can find anything because if I can remember correctly your last question was answer about a week ago.

    Good Luck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center