Thread: Exercice - Calculate the fee

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Exercice - Calculate the fee

    This is just an exercise.
    And i need to complete this.

    This is the question :

    Write a program in C++ that calculates the amount to be paid to an employee based on the hours worked and rate per hour. A user will enter hours worked and rate per hour for an employee. Your program should have a function payCheck that calculates and returns the amount to be paid. The formula for calculating the salary amount is as follows: for the first 40 hours, the rate is the given rate (the rate that a user has entered); for hours over 40, the rate is 1.5 times the given rate.


    i had write my own coding, but it keeps error. it is okay.
    Still running, but anyone can help me?
    So, this is my coding.
    The problem is i need to calculate if the employee work for more than 40 hours which 1.5 times.


    Code:
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    #include <stdio.h>
    #include <iostream.h>
    
    using namespace std;
    
    
    int main(void)
    {
    char *empname, *test, *final_output;
    float hours=0, pay_rate=0, gross=0, net=0;
    
    //loop
    do {
    /*-- get first & last name --*/
    cout<<"Please Enter Your Name ";
    cin>>empname;
    
    /*--
    get pay rate with error checking on
    null and 0 values
    --*/
    do {
    cout << "Pay Rate: ";
    cin >> test;
    if (test == NULL)
    cout << "Invalid pay rate.";
    else if(atof(test) != 0)
    break;
    else
    cout << "Invalid pay rate.";
    } while (1);
    pay_rate=atof(test);
    
    /*--
    get hours worked with error checking on
    null and 0 values
    --*/
    do {
    cout << "Hours Worked: ";
    cin >> test;
    if (test == NULL)
    cout << "Invalid amount of hours.";
    else if(atof(test) != 0)
    break;
    else
    cout << "Invalid amount of hours.";
    } while (1);
    hours=atof(test);
    
    /*-- calculate values --*/
    gross=hours*pay_rate;
    
    /*-- set out precision --*/
    cout.precision(2);
    cout.setf(ios::fixed | ios::showpoint);
    
    /*-- create output --*/
    sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n",
    empname);
    cout << final_output;
    cout << "Gross: " << gross << endl;
    
    /*-- do another? --*/
    cout << "Calculate Another? (y/n)?";
    cin >> test;
    
    /*-- check first value of string for y/Y --*/
    } while (!strncasecmp(test,"y",1));
    
    return 0;
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    fix your indentation or how you paste your code - its not very readable like that - and tell us more information about what your problem is - like what is the error? When it is running?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    This is my latest code
    It does not have an error, but i cannot calculate the rate of overtime 4O hours.
    i am still running my exercise and need help.

    Code:
    //Write a program in C++ that calculates the amount to be paid
    //to an employee based on the hours worked and rate per hour.
    //A user will enter hours worked and rate per hour for an employee.
    //Your program should have a function payCheck that calculates and
    //returns the amount to be paid. The formula for calculating
    //the salary amount is as follows: for the first 40 hours,
    //the rate is the given rate (the rate that a user has entered);
    //for hours over 40, the rate is 1.5 times the given rate.
    
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    #include <stdio.h>
    #include <iostream.h>
    
    using namespace std;
    
    int main(void)
        {
        char *empname, *test, *final_output;
        float hours=0, pay_rate=0, emprate=0;
        float pay_rate_overtime=1.5;
    
    //variable
        final_output = new char[1024];
        empname = new char[50];
        test = new char[10];
    
    //loop
        do {
            cout << "\nPlease Enter Your Name : ";
            cin >> empname;
            
        do {
            cout << "\nEnter Your Pay Rate : ";
            cin >> test;
            if (test == NULL) 
            cout << "Invalid pay rate.";
            else if(atof(test) != 0) 
            break;
            else
            cout << "please enter digit only";            
            }
        while (1);
            pay_rate=atof(test);
        
        do {
            cout << "\nEnter Your Worked Hours: ";
            cin >> test;
            if (test == NULL)
            cout << "please enter digit only";
            else if(atof(test) !=  0)
            break;
            else
            cout << "Invalid amount of hours.";
            }
        while (1);
            hours=atof(test);
    
            /*-- calculate values --*/        
            emprate=hours*pay_rate;
    
            /*-- set out precision --*/
            cout.precision(2);
            cout.setf(ios::fixed | ios::showpoint);
            
            /*-- create output --*/
            sprintf(final_output,"\n\n Your Pay Rate is : \n------------------------------\n", empname);
            cout << final_output;
            cout << "Your Salary Is : " << emprate <<endl;
    
            /*-- do another? --*/
            cout << "\n\n Calculate Another? (y/n)?";
            cin >> test;
    
            /*-- check first value of string for y/Y --*/
        } while (!strncasecmp(test,"y",1));
    
        /*-- return memory to the heap --*/
        delete [] empname;
        delete [] test;
        return 0;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Added some comments and indentation
    Code:
    // you are really in need of the new compiler
    
    #include <cstdlib> //this is the c++ equivalent of C include #include <stdlib.h>
    //do you need this??  #include <stddef.h>
    #include <cstring> //see above #include <string.h>
    //#include <stdio.h> - should not be needed in C++
    #include <iostream> //#include <iostream.h> - this incluude is WAAAY outdated
    
    using namespace std;
    
    int main(void)
    {
        char *empname, *test, *final_output; // YOY - you really need to read about std::string
        float hours=0, pay_rate=0, gross=0, net=0;
    
        //loop
        do {
            /*-- get first & last name --*/
            cout<<"Please Enter Your Name ";
            cin>>empname; // not gonna work - use std::string
    
            /*--
            get pay rate with error checking on
            null and 0 values
            --*/
            do {
                cout << "Pay Rate: ";
                cin >> test; //see above
                if (test == NULL) // No - not gonna work
                    cout << "Invalid pay rate.";
                else if(atof(test) != 0) // Hey! cin knows how to read floats, you know...
                    break;
                else
                    cout << "Invalid pay rate.";
            } while (1);
            pay_rate=atof(test); // see commant above
    
            /*--
            get hours worked with error checking on
            null and 0 values
            --*/
            do { // I'm not gonna reapeat myself
                cout << "Hours Worked: ";
                cin >> test;
                if (test == NULL)
                    cout << "Invalid amount of hours.";
                else if(atof(test) != 0)
                    break;
                else
                    cout << "Invalid amount of hours.";
            } while (1);
            hours=atof(test);
    
            /*-- calculate values --*/
            gross=hours*pay_rate;
    
            /*-- set out precision --*/
            cout.precision(2);
            cout.setf(ios::fixed | ios::showpoint);
    
            /*-- create output --*/
            sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n",
                empname); //Hey - this is C++ - use streams
            cout << final_output;
            cout << "Gross: " << gross << endl;
    
            /*-- do another? --*/
            cout << "Calculate Another? (y/n)?";
            cin >> test; //not gonna work
    
            /*-- check first value of string for y/Y --*/
        } while (!strncasecmp(test,"y",1)); // using std::string you'll get it way simplier
    
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
     char *empname, *test, *final_output;
    These variables are basically useless because they don't have any space for strings. You really should be using std::string, but if you can't, stay away from pointers if you haven't learned them.

    Code:
    sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n", empname);
    cout << final_output;
    There aren't enough strings in the sprintf() call to match all of the %s's in the format string. Not that you should be using sprintf() anyway. It's entirely not necessary.

    The problem is i need to calculate if the employee work for more than 40 hours which 1.5 times.
    Yeah you basically didn't do that part. You need to find out if someone worked more than 40 hours, and if so, the income formula is
    (40.0f * rate) + ((hours - 40.0f) * rate * 1.5f)
    Otherwise, the income formula that you already programmed will work.

    Code:
    cout << "Pay Rate: ";
    cin >> test;
    Didn't they teach you to write cin >> pay_rate; instead?

    Code:
    cout << "Hours Worked: ";
    cin >> test;
    And cin >> hours; ?

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    years ago had to do the same exercise
    here is a "simplistic" approach.
    on my laptop it won't stay open long enough
    to verify results

    I am using Dev C++ and Windows 8

    Code:
    #include <iostream>
     
    using namespace std ;
    
    
    void pay_me()
    
    
    {
        float hours, rate, pay ;
            
        cout <<"How many Hours did you Work this Fortnight? " ;
        cin >> hours ;
        
        cout <<"What is your Rate of Pay? " ;
        cin >> rate ;
        
        if (hours > 40)
           {
               pay = (hours * rate) + (hours - 40) * rate * 1.5 ;
           }
        else
           {
            pay = hours * rate ;
           }
        cout<<"You will be paid $" << pay ;
        
    }
    
    
    int main()
    {
        pay_me() ;
    }
    Last edited by BitRusty; 10-20-2013 at 02:22 AM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by BitRusty View Post
    on my laptop it won't stay open long enough
    FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by BitRusty View Post
    years ago had to do the same exercise
    here is a "simplistic" approach.
    on my laptop it won't stay open long enough
    to verify results

    I am using Dev C++ and Windows 8

    pay = (hours * rate) + (hours - 40) * rate * 1.5 ;
    Years ago, you shouldn't have used hours for the amount paid at a normal rate like you did here. That will overpay.

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    now running the program/s from inside the IDE, now staying open to see results and able to adjust code accordingly. previously running program from the resident folder

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    [code]
    if (hours > 40)
    {
    pay = (40 * rate) + (hours - 40) * rate * 1.5 ;
    [\code]

    now it shouldn't over pay


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need Exercice on Functions in C
    By Dr.Vip in forum C Programming
    Replies: 8
    Last Post: 01-03-2012, 04:37 AM
  2. need help for a programming exercice
    By thibault0613 in forum C# Programming
    Replies: 4
    Last Post: 11-12-2011, 07:38 PM
  3. help for my programming exercice
    By thibault0613 in forum C# Programming
    Replies: 5
    Last Post: 11-09-2011, 01:52 PM
  4. Calculate BMI and BMR
    By nguystep in forum C Programming
    Replies: 4
    Last Post: 10-26-2011, 02:59 AM
  5. Calculate Bandwith
    By scrapedbr in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-20-2004, 10:06 AM